SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, it's me again and I have yet ANOTHER question about comparing dates

    I have a date stored in a variable in the following format:-

    2001-05-09

    With the date evaluating to 9th May 2001 (for all you UK folks )

    I need to perform various checks on the date in my script, and was wondering how I would, for instance, check to see if the date was more than 9 days ago using just PHP.

    I know how to do it using a MySQL select (thanks Freddy!), but I cannot do it this way for various reasons.

    Any help greatly appreciated!

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm thinking it would be best to select the date (for comparason purposes) from the database as a unix timestamp:

    SELECT UNIX_TIMESTAMP(thedate) AS thedate_timestamp
    FROM tableName

    You can always use the PHP function date() to format the timestamp into a string; eg

    $dateString = date("Y-M-d", $thedate_timestamp);

    Otherwise, if you have the date as a string, you can explode it into an array and use the array values in a call to mktime() function to convert the date string into a unix timestamp.

    eg;
    PHP Code:
     $date "2001-05-09";
    $dateArray explode("-"$date);
    $dateAsTimeStamp =  mktime(0,0,0$dateArray[1], $dateArray[2], $dateArray[0]); 
    Now you can go berserk with mathematical operations on the unix timestamp.

  3. #3
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats great sid, thanks for the advice!

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Washington State
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also consider :
    PHP Code:
    $date '2001-05-09';
    $timestamp strtotime($date); 


Bookmarks

Posting Permissions

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