ldoozer Posted April 8, 2007 Share Posted April 8, 2007 Hi all, I have dates in my db which are stored as strings like this 08/01/2007 and i am trying to write a script which either says the date i pass in is in the future or in the past... can anyone help? Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/ Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 If you can seperate the day, month, and year, convert the month to textual format (example, "august"), then yes, this is very possible, and quite easy. $date = strtotime($day . $month . $year); $now = strtotime("now"); if($now >= $date) { echo "that date has passed"; } else { echo "That date has not yet passed"; } Should work fine, but remember, you need to have day, month, and year seperate and the month has to be in textual format. Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224058 Share on other sites More sharing options...
PC Nerd Posted April 8, 2007 Share Posted April 8, 2007 why not use timestamp and simply go: $now = ..... $to_check = ..... if ($to_check > $now ){echo "FUTURE!!!";} else {echo "ANCIENT HISTORY MAN!!!";} thats what id do. you could then store the timestames and do what ever formatting you wanted with it, what your currently doing really restricts formatting good luck Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224133 Share on other sites More sharing options...
ldoozer Posted April 8, 2007 Author Share Posted April 8, 2007 unfortunalty the way dates are currently stored and the way they are input through a javascript calender puts them in like this: 08/04/2007 and so I need to find a way of enterpreting that and quering it against another date. Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224218 Share on other sites More sharing options...
per1os Posted April 8, 2007 Share Posted April 8, 2007 <?php $time = strtotime('08/04/2007'); $newdate = date('Y-m-d', $time); ?> http://us.php.net/strtotime http://www.php.net/date That should help you on your merry way. Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224223 Share on other sites More sharing options...
ldoozer Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks that give me a good output but using that why wont this work : $time = strtotime('09/04/2007'); $newdate = date('Y-m-d', $time); $date = $newdate; $now = strtotime("now"); if($now >= $date) { echo "that date has passed"; } else { echo "That date has not yet passed"; } Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224232 Share on other sites More sharing options...
per1os Posted April 8, 2007 Share Posted April 8, 2007 $time = strtotime('09/04/2007'); $date = date('Y-m-d', $time); $now = date('Y-m-d', time()); if($now >= $date) { echo "that date has passed"; } else { echo "That date has not yet passed"; } www.php.net/time Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224236 Share on other sites More sharing options...
ldoozer Posted April 8, 2007 Author Share Posted April 8, 2007 Thanks loads that's done the trick Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224242 Share on other sites More sharing options...
kenrbnsn Posted April 8, 2007 Share Posted April 8, 2007 You really should be comparing UNIX timestamps not ASCII strings here: <?php $date = strtotime('09/04/2007'); $now = time(); if($date < $now) echo "That date has not yet passed"; else echo "that date has passed"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224244 Share on other sites More sharing options...
HaLo2FrEeEk Posted April 8, 2007 Share Posted April 8, 2007 Seriously, timestamps make everything easier. A timestamp is simply the number of seconds since the UNIX epoch (january 1, 1970, at 0:0:0). Use timestamps. Link to comment https://forums.phpfreaks.com/topic/46108-date-has-passed-function/#findComment-224420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.