deepaksinghkushwah Posted March 25, 2009 Share Posted March 25, 2009 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 More sharing options...
deerly Posted March 25, 2009 Share Posted March 25, 2009 Try date_default_timezone_set()? http://us3.php.net/date_default_timezone_set Link to comment https://forums.phpfreaks.com/topic/151012-time-in-php/#findComment-793355 Share on other sites More sharing options...
deepaksinghkushwah Posted March 25, 2009 Author Share Posted March 25, 2009 i used date_default_timezone_set("Asia/Kolkata") but it show me the following output 05:31:02 it should be 00:01:02 but it shows me 5 hours more difference. Link to comment https://forums.phpfreaks.com/topic/151012-time-in-php/#findComment-793360 Share on other sites More sharing options...
deepaksinghkushwah Posted March 25, 2009 Author Share Posted March 25, 2009 thanks for you kind support. it solve me problem. i just use date_default_timezone_set('Etc/GMT') for get GMT 00:00. and my problem solved. it shows correct answer. Link to comment https://forums.phpfreaks.com/topic/151012-time-in-php/#findComment-793361 Share on other sites More sharing options...
PFMaBiSmAd Posted March 25, 2009 Share Posted March 25, 2009 Use gmdate() instead of date() Link to comment https://forums.phpfreaks.com/topic/151012-time-in-php/#findComment-793364 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.