1

I've recently upgraded to PHP5, and have noticied that within an application I've built there seems to be an extra hour added to some of my variables and caculations.

I am using:

date_default_timezone_set('Europe/London');

which I understand means that PHP is using BST opposed to standard GMT, but when I print empty variables (returned before as 00:00 using "H:i") - I'm now getting 01:00.

When caculating the hour/mins difference between two datetimes - I'm also getting an extra hour.

My basic code is:

<td><?php if(isset($item->start_time)) { echo date('H:i', strtotime($item->start_time)); } ?></td>
<td><?php if(isset($item->finish_time)) { echo date('H:i', strtotime($item->finish_time)); }?></td>
<td>
     <?php
     $start = strtotime($item->start_time);
     $end = strtotime($item->finish_time);
     $elapsed = $end - $start;
     if($elapsed != NULL) { echo date("H:i", $elapsed); }
     ?>
</td>

Which for an example rown returns:

Start: 08:57 (Based on $item->start_time as 2011-03-19 08:57:23 in my DB)

Finish: 12:59 (Based on $item->finish_time as 2011-03-19 12:59:38 in my DB)

Caculation: 05:02 (This should be 04:02)

hakre
  • 193,403
  • 52
  • 435
  • 836
James P
  • 47
  • 1
  • 5

2 Answers2

1

You are treating the time difference as a point in time when you use date("H:i"). I suggest you just calculate the elapsed time difference manually

echo floor( $elapsed / 60) . ":" . ($elapsed % 60)

Otherwise you will run into problems when $elapsed is bigger then 24 hours.

Alex
  • 32,506
  • 16
  • 106
  • 171
  • This has returned a calculation of: 242:15 . Is still have the problem of empty variables returing as 01:00 in other text boxes - which would usually display database time data. For example I have a text field where you enter "Paid Hours". This is reading as 01:00 rather than 00:00 as the variable isn't set or filled. – James P Apr 21 '11 at 20:08
0

Try setting

date_default_timezone_set('UTC');

immediately before converting your elapsed time to H:i format in

if($elapsed != NULL) { echo date("H:i", $elapsed); } 

remembering to reset it again afterwards

You're treating the elapsed time as a datetime stamp for formatting (that's what the date() function expects) so PHP is following it's standard behaviour and treating it as a locale formatted date/time (not appreciating that it's being used as an elapsed time), so it's adding the hour to display the correct time according to your timezone setting.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • He will run into problems when $elapsed is bigger than 24 hours. – Alex Apr 21 '11 at 13:09
  • Setting the timezone before and changing it back again after has forced it to work out the correct calcution, but for my other input text fields on the same row such as Lunch, Paid Hours, Sickness & Holiday all display 01:00 rather than 00:00 as before - as they're values are currently NULL in the database. I'm thinking of pre-filling those fields with 00:00:00 when the row is created but wasn't sure if there was another way to get around this – James P Apr 21 '11 at 20:14