Wrox Programmer Forums
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 13th, 2003, 12:46 AM
Authorized User
Join Date: Jun 2003
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Date Comparison

Ok, this should be an easy one, though I don't know how.

Ok, I have a PHP generated date string, (you know that 10 or so number string that by itself means nothing to most of us), and I have another as well. What I want to do is compare the 2 strings and get the difference between them, i.e. hours, minutes, seconds, etc. Is this even possible? Or am I just crazy?


Old June 14th, 2003, 03:40 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo

ok so here's how you can do it. have something like

$dateold = date('Y-m-d')
$datenew = date('Y-m-d')

if you just want to compare these two just do split the time string

$dateoldArray = split("-",$dateold);
$datenewArray = split("-",$datenew);

you can subtract months for example

$monthdiff = $dateoldArray[1] - $datenewArray[2];

as of time (hours, seconds...) you can generate the string for example:

$timenow = "12:45:33";

then just repeat stuff as above. hope that helped
Old June 15th, 2003, 06:25 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn

If your dates are in the user friendly format (Y-m-d etc.)
you can use mktime function in order to change current format
to UNIX format. Newest value means number of seconds since 1.1.1970
and when you have two of these values you can easy subtract one from
the other and then convert second to H:m:s format.
function convert_this_date_to_unix_date($date)
    mktime func accept following params
    int hour, int minute, int second, int month, int day, int year
    and we have to explode $date value for the preparing for
    mktime func's usage
    $date_sections=explode(" ", $date);
    $date_section=explode("-", $date_sections[0]);
    $time_section=explode(":", $date_sections[1]);

    return mktime ($time_section[0]+$am_pm, $time_section[1], $time_section[2],
            $date_section[1], $date_section[2], $date_section[0]);

$date1="2003-05-05 10:12:23 PM"
$date2="2003-05-06 11:12:23 PM"



...but the Soon is eclipsed by the Moon
Old June 19th, 2003, 02:58 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

Is all the exploding above really necessary?? Why not just use strtotime()?


function date_diff($date_1, $date_2, $format = '')
   $ts = abs(strtotime($date_1) - strtotime($date_2));

   return ($format == '')? ts : date($format, $ts);
The optional parameter, $format, lets you choose between returning a formatted date string and a unix timestamp.

Take care,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Comparison jroxit Classic ASP Databases 5 October 5th, 2007 05:39 PM
Date comparison problem mat41 Javascript 3 August 30th, 2006 09:22 PM
Date comparison rajuru Beginning PHP 2 February 19th, 2005 10:33 AM
Date comparison lily611 General .NET 5 January 4th, 2005 07:08 AM
Date Comparison hoffmann Classic ASP Databases 7 October 21st, 2004 09:00 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.