peterjc Posted July 30, 2008 Share Posted July 30, 2008 If have two datetime. so, i want to get the number of second between those two datetime. How should i do it? Example: datetime 1 - start time: 2008-7-30 13:12:10 datetime 2 - end time: 2008-7-30 14:05:30 so, should be end time minus start time, but how to get it in second? Thank in advance. Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/ Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 From MySQL? SELECT (UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start)) AS elapsed FROM sometable WHERE id = 2 That should work. Assuming end and start are your column names. Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-603701 Share on other sites More sharing options...
genericnumber1 Posted July 30, 2008 Share Posted July 30, 2008 as the last poster pointed out, it's just subtraction. timestamps are already in "seconds since unix epoch" so after subtraction... you're done! Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-603737 Share on other sites More sharing options...
peterjc Posted July 31, 2008 Author Share Posted July 31, 2008 Thank for the reply. But, actual the end time is the current time actually. i use php to get the current time and minus the start time that store in database. So, is it possible to use php to get the second? Thank. Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-604096 Share on other sites More sharing options...
genericnumber1 Posted July 31, 2008 Share Posted July 31, 2008 yes, the seconds since the start time is the current linux timestamp minus the linux timestamp of the start time. time() - $startTime Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-604100 Share on other sites More sharing options...
peterjc Posted July 31, 2008 Author Share Posted July 31, 2008 I think that is not what i want! I just want to get the second between to date time. Example: datetime 1 - start time: 2008-7-30 13:10:10 datetime 2 - end time: 2008-7-30 13:15:10 Above example, the number of second should be 300 second right? 300 second is what i want. Thank. Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-604113 Share on other sites More sharing options...
genericnumber1 Posted July 31, 2008 Share Posted July 31, 2008 Oh, I thought you were using linux timestamps to store times, not these mysql timestamps you showed originally... I suppose that should have been obvious but I didn't see it. <?php echo strtotime('2008-7-30 13:15:10') - strtotime('2008-7-30 13:10:10'); ?> or for the current time minus a timestamp <?php echo time() - strtotime('2008-7-30 13:10:10'); ?> Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-604124 Share on other sites More sharing options...
peterjc Posted July 31, 2008 Author Share Posted July 31, 2008 Thank. It work now. Link to comment https://forums.phpfreaks.com/topic/117367-get-second-between-two-datetime/#findComment-604142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.