compare date – PHP

  php

Q(Question):

Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is earlier?

thanks and regards

Jorn

A(Answer):

jo****@yahoo.com wrote:

Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is earlier?

thanks and regards

Jorn

You can use PHP’s mktime() time fuction:

mktime ( int hour, int minute, int second, int month, int day, int year
[, int is_dst])

Simply split the date on dashes ("-") and enter the year, month, and day
in the respective places. It will return an integer value for the date,
or the time since epoch. Just compare the integers. For more
information, see: http://www.php.net/manual/en/function.mktime.php

Antoine

A(Answer):

jo****@yahoo.com wrote:

How do i compare two date in YYYY-MM-DD format to determine which one is
earlier?

Because the order is year then month then day with leading zeros, you can
just do a straight string comparison on them.

if($date1 < $date 2) {
… date1 is earlier than date2 …
}
elseif($date1 == $date2) {
… they are the same …
}
else {
… date2 is earlier than date1
}

Chris


Chris Hope
The Electric Toolbox Ltd
http://www.electrictoolbox.com/

A(Answer):

<?php
function compareDates($date1,$date2) {
$date1_array = explode("-",$date1);
$date2_array = explode("-",$date2);
$timestamp1 =
mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);
$timestamp2 =
mktime(0,0,0,$date2_array[1],$date2_array[2],$date2_array[0]);
if ($timestamp1>$timestamp2) {
print "The second date is earlier than the first.";
} else if ($timestamp1<$timestamp2) {
print "The first date is earlier than the second.";
} else {
print "The dates are equal.";
}
}
compareDates("2004-03-06","2004-05-06");
// outputs "The first date is earlier than the second."
?>

of course this doesnt test for the validity of the dates (for example if the
user inputs dates via a form and the dates are invalid, an error could
result)

– JP

<jo****@yahoo.com> wrote in message
news:e9**************************@posting.google.c om…

Hi,

How do i compare two date in YYYY-MM-DD format to determine which one is
earlier?
thanks and regards

Jorn

A(Answer):

I noticed that Message-ID: <vYJcc.208563$po.1043099@attbi_s52> from
kingofkolt contained the following:

$timestamp1 =
mktime(0,0,0,$date1_array[1],$date1_array[2],$date1_array[0]);

strtotime() is easier, but Chris’s method is even easier still.


Geoff Berrow (put thecat out to email)
It’s only Usenet, no one dies.
My opinions, not the committee’s, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

A(Answer):

Chris Hope <bl*******@electrictoolbox.com> wrote in message news:<40********@news.athenanews.com>…

jo****@yahoo.com wrote:

How do i compare two date in YYYY-MM-DD format to determine which one is
earlier?

Because the order is year then month then day with leading zeros, you can
just do a straight string comparison on them.

if($date1 < $date 2) {
… date1 is earlier than date2 …
}
elseif($date1 == $date2) {
… they are the same …
}
else {
… date2 is earlier than date1
}

FWIW, I added similar thoughts in the manual’s usernotes section
sometimes ago <http://groups.google.com/groups?selm=note-33252%40php.net>
But, it was get deleted 🙁


http://www.sendmetoindia.com – Send Me to India!
Email: rrjanbiah-at-Y!com

A(Answer):

> strtotime() is easier, but Chris’s method is even easier still

i had thought there was something to convert a string to a timestamp. i just
couldnt remember what it was lol, so i wasnt sure if it existed.

– JP

LEAVE A COMMENT