PJHOPE Posted February 6, 2007 Share Posted February 6, 2007 I have a fairly simple question. I was working on a PHP script which pulled a time stamp from a database. My problem was in getting the time stamp from MySQL in the format 2007-01-29 12:12:12 to be subtracted from the current time and converted into a decimal representing hours (12.88 hrs). Then the current time is inserted back into the DB as the time stamp. Honestly it is just conversion between MySQL standard and the PHP but I cannot find any solutions in any of the books or online guides I have. Any help would be appreciated particularly some code. Thanks all, Joe Link to comment https://forums.phpfreaks.com/topic/37370-php-derived-time-data/ Share on other sites More sharing options...
hvle Posted February 7, 2007 Share Posted February 7, 2007 $mysqlTS = "2005-12-01 12:12:12"; // given a mysql timestamp $numOfSeconds = strtotime($mysqlTS) - time(); // number of seconds from that date to now $numOfHours = $numOfSeconds/(3600); echo "$numOfHours hrs"; If $mysqlTS is in the past, you will get negative number of hours. Link to comment https://forums.phpfreaks.com/topic/37370-php-derived-time-data/#findComment-178736 Share on other sites More sharing options...
PJHOPE Posted February 7, 2007 Author Share Posted February 7, 2007 Great! Thanks for the help. Now the only problem is putting the current time into the attribute in the right format. Is there a timetostr function? If so how do you set the format of the string produced? Link to comment https://forums.phpfreaks.com/topic/37370-php-derived-time-data/#findComment-178763 Share on other sites More sharing options...
hvle Posted February 7, 2007 Share Posted February 7, 2007 echo date('Y-m-d h:i:s'); will display current time in output: 2007-02-07 01:30:25 Alternatively, you can use mysql's NOW() function to create current time directly into database. Link to comment https://forums.phpfreaks.com/topic/37370-php-derived-time-data/#findComment-178778 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.