Jump to content

Today - Past Date = Time in seconds


DomMarx

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/279130-today-past-date-time-in-seconds/
Share on other sites

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.

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 :P

 

Thanks again man. I appreciate it!

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). 

$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();

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.