Comparing datetimes and timezone issue

Posted on 2007-10-08
Last Modified: 2009-07-29
I have the following code to retrieve a date and compare with the present time, which works, but what if I want to say as long as it's still today, count it as the future, even though the time might have past already.

Also, I'm not too sure how to handle time zones with this, as $commenceDate, which is stored in MySQL database  and refers to a GMT +8 timezone. I'm using PHP 4.3.

$commenceDateTime = $row['commenceDateTime']; //read from MySQL datetime field
$commenceDateTime = strtotime( $commenceDateTime );

$currentDateTime = mktime();

if ($currentDateTime >= $commenceDateTime) {
   echo "past";

else {
   echo "future";
Question by:skylabel
    1 Comment
    LVL 21

    Accepted Solution

    To solve the timezone problem, when the $commenceDateTime is retrieved from the database, add 8 hours (or add/subtract however many you like to match your timezone) like this:

    $commenceDateTime = $row['commenceDateTime'];
    $commenceDateTime = strtotime("+8 hours", $commenceDateTime); // Adds 8 hours to the time from the database

    As for your if it is today, count in the future we can write a little function like this which returns 0 if it is today:

    function count_day($start_date, $end_date) {
       $start_date_unixtimestamp = strtotime($start_date);
       $start_date_day = date("d", $start_date_unixtimestamp);
       $end_date_unixtimestamp = strtotime($end_date);
       $end_date_day = date("d", $end_date_unixtimestamp);
       $calculated_date_unixtimestamp = $start_date_unixtimestamp;
       while ($calculated_date_unixtimestamp < $end_date_unixtimestamp)  
           $calculated_date_unixtimestamp = strtotime($start_date . " +{$counter} days");
       if ( ($counter==1) && ($end_date_day==$start_date_day)) $counter=($counter-1);
       return $counter;

    Calling something like this:
    count_day("2004-04-24", "2004-04-25");
    Would return 1. And something like this:
    count_day("2004-04-24", "2004-04-24");
    would return 0.

    Hope this helps.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now