Jump to content

Today - Past Date = Time in seconds


Go to solution Solved by Lukeidiot,

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!

Edited by DomMarx
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). 

Edited by DomMarx
  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.