Re: date comparison

From: Date: Mon, 15 Jan 2001 18:39:13 +0000
Subject: Re: date comparison
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-34566@lists.php.net to get a copy of this message
all right, now I have both date values in the same format (yyyy/mm/dd), say $Date1 = 20010115 and $Date2 =20010120. If what I want is to find out if $Date1 come before $Date2, can I just use this sniplet below? if ($Date1 < $Date2) { ........... } else { ....... } Do I still need to use mktime() for this purpose? cheers Jack jacky@activelifestyle.com "There is nothing more rewarding than reaching the goal you set for yourself" ----- Original Message ----- From: Jason Murray <Jason.Murray@melbourneit.com.au> To: 'Jacky@lilst' <jacky@activelifestyle.com>; <php-general@lists.php.net> Sent: Sunday, January 14, 2001 11:37 PM Subject: RE: [PHP] date comparison > > can I used the value from that variables to compare with another > > date value? Say another date value I will use is also retrieved > > from a field in table which is in Date data type as well. > > When you get into this stuff, it all starts getting a lot more complicated. > > If you want to compare two fields, make sure they're in the same format. > > Basically, make sure you use four digit years and two digit months and > days to create your birthday fields, so they look like (for example, with > today's date, 20010115 instead of 2001115 - that second one could really > be anything). > > If you're sure you're storing the birthdays properly, you can then cut > the strings up when you pull them out of the database, the first 4 chars > are the year, the next two make the month (regardless of the actual month, > this way it'll be "01" not "1"), then the day. > > Do that for both of the dates. > > Now, you can use the mktime() command to turn them into unix tiumestamps. > > $unixtimeme = mktime(myhour, myminute, mysecond, mymonth, myday, myyear); > $unixtimethem = mktime(theirhour, theirminute, theirsecond, theirmonth, > theirday, theiryear); > > Now, a unix timestamp is the number of seconds from 00:00:01, Jan 1, 1970. > > You can figure out the difference in seconds between the two timestamps. > > Divide it by (24*3600), which is the number of seconds in a day, and there > you have the number of days between the dates. > > Incidentally, if you want to find the number of days between your birthDAYS, > then you'll want to substitute in a specific year in the mktime() statements > above, as you'll otherwise end up with the number of days between your exact > DATES of birth. > > Jason > > -- > Jason Murray > jasonm@melbourneit.com.au > Web Design Team, Melbourne IT > Fetch the comfy chair! >

« previous php.general (#34566) next »