DomMarx Posted June 13, 2013 Share Posted June 13, 2013 (edited) Hey guys, Does anyone know a simple ,clean way of getting the time between a past date and todays date in seconds? I'm trying to figure out what the Linux Timestamp is about. I know it's the time since 1970 to now, but I don't see how I could use that here or why it's used. Perhaps someone could also give me a clue as to what it's mostly used for. THANKS! Edited June 13, 2013 by DomMarx Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/ Share on other sites More sharing options...
Lukeidiot Posted June 13, 2013 Share Posted June 13, 2013 $now = time(); This is the number of seconds since 1970. $past_date = strtotime(date('m-y-d')); $past_date - $now = $sec_inbetween; (Something like that) Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435854 Share on other sites More sharing options...
requinix Posted June 13, 2013 Share Posted June 13, 2013 The Unix timestamp is a very simple way of expressing times that is not affected by timezones. You can easily subtract timestamps like Luke showed to get the elapsed time* between them. Naturally you can also add elapsed times to that without needing special logic about converting between units or dealing with leap years... at least not until you convert it back to a date string. The downside is that it can require large numbers (see the Y2K38 bug), there was iffy support for negative timestamps (pre-1970), and there still is iffy support for fractional timestamps (like precision milliseconds). There's also a lot of debate about when it's better to use a Unix timestamp or a date string, especially when it comes to databases. * Note elapsed time. With daylight savings, adding 86400 seconds (one day) may not give you the same HH:MM:SS time as you started with. Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435857 Share on other sites More sharing options...
DomMarx Posted June 13, 2013 Author Share Posted June 13, 2013 Hi Luke, Thanks for the feedback. I'm having a bit of trouble understanding why we use time();? If it's the time between now and January 1st 1970 in seconds, how would that give me the time between date1 and date2? Let's say I want the time in seconds between right now and 6:43PM June 3rd 2011 in seconds? Not between 1970 and June 3rd 2011 Thanks again man. I appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435859 Share on other sites More sharing options...
DomMarx Posted June 13, 2013 Author Share Posted June 13, 2013 (edited) Hi requinix, Thanks for the explanation man! I didn't know that. I'll be using this function to get me the "x time ago" since a posted comment. I already wrote up the function to get the number of seconds into x seconds, minutes, days, etc... I just need a way to get me the number of seconds between the date posted and the date viewed(current time/date). Edited June 13, 2013 by DomMarx Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435861 Share on other sites More sharing options...
Solution Lukeidiot Posted June 13, 2013 Solution Share Posted June 13, 2013 $timeFirst = strtotime('2011-05-12 18:20:20');$timeSecond = strtotime('2011-05-13 18:20:20'); $differenceInSeconds = $timeSecond - $timeFirst; You arent calculating the time between 1970 and now, you are calculating the seconds that have passed between those two dates. So if you get 60 seconds, then you can convert that to 1 minute or $seconds / 60; strtotime() just converts it to time(); Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435862 Share on other sites More sharing options...
DomMarx Posted June 13, 2013 Author Share Posted June 13, 2013 (edited) That's exactly what I needed Luke! Yeah, I wrote up a function to convert everything. I just needed those 3 lines of code you gave me! Thank you! Edited June 13, 2013 by DomMarx Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435866 Share on other sites More sharing options...
DomMarx Posted June 14, 2013 Author Share Posted June 14, 2013 heum, ok? lol Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435881 Share on other sites More sharing options...
requinix Posted June 14, 2013 Share Posted June 14, 2013 heum, ok? lolIt was spam, ignore it. Quote Link to comment https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/#findComment-1435884 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.