How do I tell if a time is 36 hours past another time and date?
Here's my problem. I have a date an time format that looks like this:
06-01-2012, 10:15am (MM-DD-YYYY, HH:MMxx).
What I want to do is create code (using PHP, preferably) that checks whether this date and time is later than 36 hours from now.
For example, if today's April 29th at 11:00am, I want to report one thing if it's before April 30th at 11:00pm and another if it's equal or later.
Any ideas? If I should convert this to a different format, how would I do that?
You could convert your passed date to a UNIX timestamp (strtotime() at the simplest, or a more advances usage of mktime() or gmmktime())
You also know what the UNIX timestamp is in 36 hours (thanks to mktime() or gmmktime())
From there, it's simple arithmetic to know if the two dates are more or less than 129600 seconds apart (60sec*60min*36hours).
Last edited by OctoberWind; 05-15-2013 at 01:29 PM.
Reason: math fail... too many numbers on screen
Great! Thank you so much! That sounds simple enough.
So I have this data coming through as a string (all one string). Any idea how I convert that to the proper format?
Sorry... I'm new to timestamping.
That's what strtotime() will do for you: you pass it a string representing a date-time -- it recognizes most common formats -- and it converts it to a UNIX timestamp integer in seconds.
If using PHP 5.3.0+, you could instead use the DateTime::diff() method.
ONE MORE THING!
(Thanks, you've been really helpful so far.)
Is there a way to tell if a date and time is earlier than another date?
For example, is $date1 before $date2?
If you have converted it into a UNIX timestamp via strtotime(), then it's simply an integer number of seconds since the "UNIX epoch", which you can compare just like any other integers (with "<" and ">").
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread