Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011

    Comparing dates in PHP

    Hey guys,

    I need a little help if you could, what I need is a way to compare dates in PHP.

    Basically I need to check against my database and get a record checking when that specific user last visited a page, and if the have accessed it within the last 24 hours run one script, and if not then run another script. If that makes any sense.

    Any help appreciated.


  2. #2
    Join Date
    Jan 2006
    Athens, Greece
    Work with time().

    When the user visits the "page" you log to your db his ip, browser string and anything else
    that can identify him as unique visitor (although this is only possible with a cookie) and the time ( time() ) he view the "page". Then once he comes back you identify him, retrieve his data
    from the db and call again time to do a time difference check:
    One day is 86400 seconds, so you subtract both times and see if the difference is larger than 86400. ie:

    PHP Code:
    $old_time 100000// time from db record
    $time_now time();
    $diff time() - $old_time;
    if (
    $diff 86400)
    // after one day visit
    // not 24 hrs passed

  3. #3
    Very good, but perhaps a bit of an explanation would be nice, so he understands what he is doing.

    First, let's explain the PHP time() command. MAN explains the time function this way: "Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). "

    Basically what this means, is the time returned by the php time() function is the seconds since January 1, 1970. This method is used by unix systems and is a standard way to calculate computer time.

    This being said, it is useful to have the following figures in your head when messing with unix times, since everything is in seconds.

    1 minute = 60 seconds
    1 hour = 3600 seconds
    1 day = 86400 seconds
    1 year (@ 365 days) = 31536000

    Now that you understand how the time works, let's do what you want.

    PHP Code:
    //here you fetch the user info from the database and store the last visit time in $lastTime

    if ((time() - $lastTime) > 86400// if the current "time()" - $lastTime is greater than one day
    // do code
    } else {
    //do code

    If you have the date stored in the database as as timestamp, make sure to fetch it with from_unixtime.

Similar Threads

  1. [PHP] Playing with dates
    By Tomer in forum Programming Tutorials
    Replies: 6
    Last Post: 01-13-2006, 09:31 PM
  2. How to display dates in database with php
    By shwetank in forum Programming Discussion
    Replies: 5
    Last Post: 01-02-2006, 01:47 AM
  3. PHP: Dates
    By mylinear in forum Programming Discussion
    Replies: 3
    Last Post: 09-23-2005, 04:09 PM
  4. [php]converting dates
    By kneuf in forum Programming Discussion
    Replies: 5
    Last Post: 10-29-2004, 01:55 AM
  5. Dates in PHP???
    By PaulH in forum Programming Discussion
    Replies: 1
    Last Post: 09-22-2002, 02:50 PM

Posting Permissions

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