kjtocool Posted January 1, 2008 Share Posted January 1, 2008 I am trying to compare today's date, which I get with a getdate() function, and a date stored in a database. The dates are: Today's Date: 2008-1-1 9:31:18 Latest Deadline: 2008-01-04 00:01:00 The comparison I do is: <?php if ($today_date < $latest_prediction_deadline) { ?> I have echoed the values, both are exactly as they look above. It was working fine until today, but now it is going to the else statement. My guess is that the issue stems from the fact that today's date doesn't have a 0 in front of the day, but could that cause this? If so, how can I modify the getdate() function to return values like 01, 02, etc for days and months? This is how I currently set the $today_date variable: <?php $today = getdate(); $today_year = $today['year']; $today_month = $today['mon']; $today_day = $today['mday']; $today_hours = $today['hours']; $today_minutes = $today['minutes']; $today_seconds = $today['seconds']; $today_date = $today_year . "-" . $today_month . "-" . $today_day . " " . $today_hours . ":" . $today_minutes . ":" . $today_seconds; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2008 Share Posted January 1, 2008 You can use the sprintf() function with a "%02d" format parameter or you can use the str_pad() function. However, since you are doing this with a DATETIME column in a database, just use the mysql now() function in your query. There is no need for any php code. Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/#findComment-427293 Share on other sites More sharing options...
kjtocool Posted January 1, 2008 Author Share Posted January 1, 2008 Interesting. So I currently have a query that is: $query = "SELECT * FROM bonanza_weeks ORDER BY prediction_end DESC LIMIT 1"; Could I change that to: $query = "SELECT Now() AS date, some_column, another_column FROM bonanza_weeks ORDER BY prediction_end DESC LIMIT 1"; Would that return the date as 'date' in my associative result array? I did get the code working using pad: <?php $today = getdate(); $today_year = $today['year']; $today_month = $today['mon']; $today_month = str_pad($today_month, 2, "0", STR_PAD_LEFT); $today_day = $today['mday']; $today_day = str_pad($today_day, 2, "0", STR_PAD_LEFT); $today_hours = $today['hours']; $today_minutes = $today['minutes']; $today_seconds = $today['seconds']; $today_date = $today_year . "-" . $today_month . "-" . $today_day . " " . $today_hours . ":" . $today_minutes . ":" . $today_seconds; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/#findComment-427300 Share on other sites More sharing options...
duclet Posted January 1, 2008 Share Posted January 1, 2008 You are better off using time() instead of getdate(). Then you can simply use strtotime() to convert the deadline date into a Unix timestamp. <?php $today = time(); $deadline = strtotime('2008-01-04 00:01:00'); printf('Current Timestamp: %s<br />Deadline Timestamp: %s<br />', $today, $deadline); if($today < $deadline) { echo 'true'; } else { echo 'false'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/#findComment-427303 Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2008 Share Posted January 1, 2008 If you want rows from your table where prediction_end is greater than the current date/time - $query = "SELECT some_column, another_column FROM bonanza_weeks WHERE prediction_end > now() ORDER BY prediction_end DESC LIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/#findComment-427312 Share on other sites More sharing options...
kjtocool Posted January 1, 2008 Author Share Posted January 1, 2008 If you want rows from your table where prediction_end is greater than the current date/time - $query = "SELECT some_column, another_column FROM bonanza_weeks WHERE prediction_end > now() ORDER BY prediction_end DESC LIMIT 1"; Ooh, I really like that. Thanks everyone for your help. I'm a bit hungover to be coding now, I'll take a look and implement some of your suggested changes later on. Quote Link to comment https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/#findComment-427318 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.