Jump to content

Time in php


Recommended Posts

hi all, i have  a strange problem regarding time in php. I m using following code for subtract hours.....

 

<?php

$t1=strtotime('16:00:01');

$t2=strtotime('16:01:03');

$t2=date("H:i:s",$t2-$t1);

echo $t2

?>

 

 

output : 00:01:02

 

above code working absolutely fine on localhost. but when i upload it to my webserver [a live server located in DALAS, TAXES]. it show me the wrong answer. it show me 18:01:02. It shows 18 hours difference but the corrent is just 1 minute 2 second because i m subtracting time.

 

please help me to find out the correct answer.

 

Thanks and Regards

Link to comment
https://forums.phpfreaks.com/topic/151012-time-in-php/
Share on other sites

I had a similar problem yesterday when adding up amounts of time spent working on assignments stored in a database.

 

In the end I stopped using strtotime() and wrote a function to convert the time into seconds.

  function convertTimeToSeconds($t) {
  	$hrs=intval(substr($t,0,2))*3600;  //extract HH:mm:ss
  	$min=intval(substr($t,3,2))*60;    //extract hh:MM:ss
  	$sec=intval(substr($t,6,2));       //extract hh:mm:SS
  	return ($hrs+$min+$sec);
  }

 

Then I would use it like this:

$t1=convertTimeToSeconds('16:00:01');
$t2=convertTimeToSeconds('16:01:03');
$t3=$t2-$t1;
echo date('H:i:s',$t3);

Link to comment
https://forums.phpfreaks.com/topic/151012-time-in-php/#findComment-793402
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.