dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: How do I tell if a time is 36 hours past another time and date?

  1. #1
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372

    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?

    Thanks.

  2. #2
    Join Date
    Jul 2007
    Location
    Wisconsin
    Posts
    468
    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 02:29 PM. Reason: math fail... too many numbers on screen

  3. #3
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    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.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    372
    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?

    Thanks.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    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 ">").
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles