Jump to content

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/83970-solved-problem-comparing-dates/
Share on other sites

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.

 

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;
?>

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'; }
?>

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";

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.