Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2003, 12:46 AM
Authorized User
Join Date: Jun 2003
Location: San Jose, CA, USA.
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?


Reply With Quote
  #2 (permalink)  
Old June 14th, 2003, 03:40 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
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
Reply With Quote
  #3 (permalink)  
Old June 15th, 2003, 06:25 AM
Friend of Wrox
Join Date: Jun 2003
Location: Wien, Wien, Austria.
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
Reply With Quote
  #4 (permalink)  
Old June 19th, 2003, 02:58 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: San Diego, CA, USA
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,

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 09:04 PM.

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