473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

compare date

Hi,

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

thanks and regards

Jorn
Jul 17 '05 #1
6 16219
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
Jul 17 '05 #2
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/
Jul 17 '05 #3
<?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

Jul 17 '05 #4
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/
Jul 17 '05 #5
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
Jul 17 '05 #6
> 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
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
1
by: vrparekh | last post by:
in my webapplication,user pick date in dd/mm/yy format, and i want to compare that date with another date using compare validator but compare validator compare the dates using mm/dd/yy format. ...
2
by: deepikashalini | last post by:
hi, Im storing Issue date in database. Issuedate stored as varchar. and then compare to Passing string date('03/06/2008') what is query to compare storing varchar date to passing string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.