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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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"; } Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.