Re: [PHP] comparing dates
Is there a quick way to compare dates in the format dd/mm/yy without exploding and comparing the individual parts? Compare them in what way? Before, after, days between? In any case, i'd look at strtotime() to convert them into timestamps, then diff them to get the number of seconds b/n them, then go from there. If strtotime() won't do it because it expects mm/dd/yy (consider 01/02/05 is that feb 1st or jan 2nd?) then split up the string and use mktime(). good luck. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] comparing dates
Is there a quick way to compare dates in the format dd/mm/yy without exploding and comparing the individual parts? R. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] comparing dates
Hey, Thanks for replying. I did find an easier way, but only after going through what you sent me...the way i found was using substr. I do read the manual, but not the online one, I have a downloaded windows helpfile copy, its much faster and easier to access but one disadvantage is...it does not have the user comments. Cheers, -Ryan http://Bestwebhosters.com - Original Message - From: "Justin French" <[EMAIL PROTECTED]> To: "Ryan A" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Monday, February 02, 2004 1:21 AM Subject: Re: [PHP] comparing dates > On Monday, February 2, 2004, at 10:04 AM, Ryan A wrote: > > > which gives me two 14 numberic characters strings like: > > 20040202091212 > > 20040201070500 > > > > How do I compare it to display something like this to the visitor: > > You have $xdays day/s, $xhours hours and $xmins minutes before your > > account > > expires > > > > Been hitting the manual, but have either been searching in the wrong > > places > > or? > > I think I will have to use "explode","strtotime" on this...but am not > > sure. > > Ryan, > > explode() won't help (nothing to explode on) > strtotime() won't help, as it can't work directly with the above date > format > > There's a decent comment on http://php.net/strtotime (RTFM) by > 'tobi at schluer dot de' > "...You can read the db using the UNIX_TIMESTAMP()-function, which > returns the timestamp from the date(time) field. So you don't need to > convert the dates in PHP, but let SQL do that job." > > So, you can do it with MySQL. Otherwise, a function like this in PHP > would do it: > > function mysqlToUnixStamp($in) > { > $y = substr($in,0,4); > $m = substr($in,4,2); > $d = substr($in,6,2); > $h = substr($in,8,2); > $i = substr($in,10,2); > $s = substr($in,12,2); > return mktime($h,$i,$s,$m,$d,$y); > } > ?> > > So then pass your mysql stamps to it like this: > > $d1 = '20040202091212'; > $d2 = '20040201070500'; > > $d1 = mysqlToUnixStamp($d1); > $d2 = mysqlToUnixStamp($d2); > ?> > > An you can then compare them (for example): > > if($d1 >= $d2) { /*something*/ } > ?> > > Or find the difference in seconds: > > $diff = $d1 - $d2; > ?> > > Converting this into a human readable "$xdays day/s, $xhours hours and > $xmins minutes" is not exactly easy, but here's some hints for you to > build on: > > function secondsToString($in) > { > $secsInDay = (60 * 60) * 24; > $secsInHour = 60 * 60; > $secsInMin = 60; > $ret = ''; > > if($in > $secsInDay) > { > $days = round($in / $secsInDay); > $remainder = $in % $secsInDay; > $ret .= "{$days} days, "; > } > > if($remainder > $secsInHour) > { > $hours = round($remainder / $secsInHour); > $remainder = $in % $secsInHour; > $ret .= "{$hours} hours, "; > } > > if($remainder > $secsInMin) > { > $mins = round($remainder / $secsInMin); > $remainder = $in % $secsInMin; > $ret .= "{$mins} mins, "; > } > > // strip off last ', ' > $ret = substr($ret,0,-2); > > return $ret; > } > > // converting to human readable > $timeLeft = secondsToString($diff); > > // print to screen > echo "you have {$timeLeft} remain on your membership"; > ?> > > > Out of all this, what you need to look-up in the manual and understand > is the modulus (%), an arithmetic operator & substr(). > > http://www.php.net/manual/en/language.operators.arithmetic.php > http://www.php.net/substr > > > Justin French > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] comparing dates
On Monday, February 2, 2004, at 10:04 AM, Ryan A wrote: which gives me two 14 numberic characters strings like: 20040202091212 20040201070500 How do I compare it to display something like this to the visitor: You have $xdays day/s, $xhours hours and $xmins minutes before your account expires Been hitting the manual, but have either been searching in the wrong places or? I think I will have to use "explode","strtotime" on this...but am not sure. Ryan, explode() won't help (nothing to explode on) strtotime() won't help, as it can't work directly with the above date format There's a decent comment on http://php.net/strtotime (RTFM) by 'tobi at schluer dot de' "...You can read the db using the UNIX_TIMESTAMP()-function, which returns the timestamp from the date(time) field. So you don't need to convert the dates in PHP, but let SQL do that job." So, you can do it with MySQL. Otherwise, a function like this in PHP would do it: So then pass your mysql stamps to it like this: $d1 = mysqlToUnixStamp($d1); $d2 = mysqlToUnixStamp($d2); ?> An you can then compare them (for example): = $d2) { /*something*/ } ?> Or find the difference in seconds: Converting this into a human readable "$xdays day/s, $xhours hours and $xmins minutes" is not exactly easy, but here's some hints for you to build on: $secsInDay) { $days = round($in / $secsInDay); $remainder = $in % $secsInDay; $ret .= "{$days} days, "; } if($remainder > $secsInHour) { $hours = round($remainder / $secsInHour); $remainder = $in % $secsInHour; $ret .= "{$hours} hours, "; } if($remainder > $secsInMin) { $mins = round($remainder / $secsInMin); $remainder = $in % $secsInMin; $ret .= "{$mins} mins, "; } // strip off last ', ' $ret = substr($ret,0,-2); return $ret; } // converting to human readable $timeLeft = secondsToString($diff); // print to screen echo "you have {$timeLeft} remain on your membership"; ?> Out of all this, what you need to look-up in the manual and understand is the modulus (%), an arithmetic operator & substr(). http://www.php.net/manual/en/language.operators.arithmetic.php http://www.php.net/substr Justin French -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] comparing dates
Is it possible to pull that dates out as unix time stamps? Then all you need to do is subtract one from the other, convert the results to days/hours/minutes and Bob's your uncle. If not, then you'll need to do some string manipulation to get the string into a format that strtotime() understands and repeat the above.. HTH Martin > -Original Message- > From: Ryan A [mailto:[EMAIL PROTECTED] > Sent: Monday, 2 February 2004 10:04 AM > To: [EMAIL PROTECTED] > Subject: [PHP] comparing dates > > > Hi, > Am a bit confused as to how to do this, I have some dates in > the database as > "expire_login timestamp(14)". > I am entering dates 1 day,11 hours,59 minutes in advance. > The user can sign in anytime and it should display how many > days, hours and > mins before his account expires... > > I am selecting the data as "select expire_login, now() from > allow_logins"; > > which gives me two 14 numberic characters strings like: > 20040202091212 > 20040201070500 > > How do I compare it to display something like this to the visitor: > You have $xdays day/s, $xhours hours and $xmins minutes > before your account > expires > > Been hitting the manual, but have either been searching in > the wrong places > or? > I think I will have to use "explode","strtotime" on > this...but am not sure. > > Any help appreciated. > > Thanks, > -Ryan > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > __ Information from NOD32 1.614 (20040129) __ > > This message was checked by NOD32 for Exchange e-mail monitor. > http://www.nod32.com > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] comparing dates
Hi, Am a bit confused as to how to do this, I have some dates in the database as "expire_login timestamp(14)". I am entering dates 1 day,11 hours,59 minutes in advance. The user can sign in anytime and it should display how many days, hours and mins before his account expires... I am selecting the data as "select expire_login, now() from allow_logins"; which gives me two 14 numberic characters strings like: 20040202091212 20040201070500 How do I compare it to display something like this to the visitor: You have $xdays day/s, $xhours hours and $xmins minutes before your account expires Been hitting the manual, but have either been searching in the wrong places or? I think I will have to use "explode","strtotime" on this...but am not sure. Any help appreciated. Thanks, -Ryan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Comparing Dates
$timestamp = time(); $yourdate="12:35:20:7:4:2001"; // 12:35:07 July 4 2001 $yd_expl = explode(':',$yourdate); if (mktime(0,0,0,strftime('%m',$timestamp),strftime('%d',$timestamp),strftime('%Y',$timestamp) < mktime($yd_expl [0],$yd_expl[1],$yd_expl[2],$yd_expl[3],$yd_expl[4],$yd_expl[5]) ){ // do you have to do } HTH Regards, Andrey Hristov IcyGEN Corporation http://www.icygen.com BALANCED SOLUTIONS - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, December 09, 2001 3:16 AM Subject: [PHP] Comparing Dates I'm trying to do a check on a date, to see if it's after the current date, so basically i'm doing an if(date("d-m-y")>"15-12-01") { do something } I know that's obviously not the right way to do it, but can someone help me out? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Comparing Dates
Absolutely you are doing in the right way:) But pls make sure that string comparison is from left to right. For example, "16" is larger than "15-12-01". Alex - Original Message - From: <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, December 08, 2001 8:16 PM Subject: [PHP] Comparing Dates I'm trying to do a check on a date, to see if it's after the current date, so basically i'm doing an if(date("d-m-y")>"15-12-01") { do something } I know that's obviously not the right way to do it, but can someone help me out? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Comparing Dates
you could pass the values to mktime and compare -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Saturday, December 08, 2001 8:17 PM To: [EMAIL PROTECTED] Subject: [PHP] Comparing Dates I'm trying to do a check on a date, to see if it's after the current date, so basically i'm doing an if(date("d-m-y")>"15-12-01") { do something } I know that's obviously not the right way to do it, but can someone help me out? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Comparing Dates
I'm trying to do a check on a date, to see if it's after the current date, so basically i'm doing an if(date("d-m-y")>"15-12-01") { do something } I know that's obviously not the right way to do it, but can someone help me out?
Re: [PHP] comparing dates works sometimes and fails another time!
You are doing a string compare not date, convert them to a unix time stamp and then compare $d1 = explode("-",$date1); $d2 = explode("-",$date2); $ts1 = mktime(0,0,0,$d1[1],$d1[2],$d1[0]); $ts2 = mktime(0,0,0,$d2[1],$d2[2],$d2[0]); if( $ts1 > $ts2 ): echo "Date 1 is greater than date2"; elseif($ts1 == $ts2): echo "date 1 is equal to date 2" ; else: echo " date 2 is greater than date 1"; endif; should work :) Tom At 09:50 AM 28/03/01 +, kaab kaoutar wrote: >Hi! > >$date1="2001-1-7";$date2="2001-1-14"; >if ($date1>=$date2) echo "date1 is greater then date2"; >else echo "date2 is greater then date1"; >the result is the first :( >but it works for other dates! > >Thanks > >_ >Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] >To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] comparing dates works sometimes and fails another time!
What are appropriate functions to do Date and Time comparison? like: Date difference Date Comparison? Adding 'n' days to current date string value, Removing 'n' month, etc. Where can i find the date integer enconding used in PHP? Like i know how it's done in Delphi (as stored as a Double value) but in PHP, the date integer how is it encoded? thanks. "Renze Munnik" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > If you _do_ want to use this method for comparing dates (which isn't > realy a usual way of doing it... there date-functions) you should > fill up with leading 0's, like: > > $date1="2001-01-07"; $date2="2001-01-14"; > > The reason is _very_ simple... You performing a text-compare here. > And let's face it... the characters "-1" (day of date2) are > "smaller" than "-7" (day in date1). > > So... > > RenzE > > > kaab kaoutar wrote: > > > > Hi! > > > > $date1="2001-1-7";$date2="2001-1-14"; > > if ($date1>=$date2) echo "date1 is greater then date2"; > > else echo "date2 is greater then date1"; > > the result is the first :( > > but it works for other dates! > > > > Thanks > > > > _ > > Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] comparing dates works sometimes and fails another time!
If you _do_ want to use this method for comparing dates (which isn't realy a usual way of doing it... there date-functions) you should fill up with leading 0's, like: $date1="2001-01-07"; $date2="2001-01-14"; The reason is _very_ simple... You performing a text-compare here. And let's face it... the characters "-1" (day of date2) are "smaller" than "-7" (day in date1). So... RenzE kaab kaoutar wrote: > > Hi! > > $date1="2001-1-7";$date2="2001-1-14"; > if ($date1>=$date2) echo "date1 is greater then date2"; > else echo "date2 is greater then date1"; > the result is the first :( > but it works for other dates! > > Thanks > > _ > Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] comparing dates works sometimes and fails another time!
Hi! $date1="2001-1-7";$date2="2001-1-14"; if ($date1>=$date2) echo "date1 is greater then date2"; else echo "date2 is greater then date1"; the result is the first :( but it works for other dates! Thanks _ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]