How can I work out if a date is on or before today?

My web application consists of library type system where books have due dates.

I have the current date displayed on my page, simply by using this:

date_default_timezone_set('Europe/London');

$date = date;
print $date("d/m/Y");

I have set 'date' as a variable because I'm not sure if it makes a difference when I use it in the IF statement you're about see, on my library books page.

On this page, I am simply outputting the due dates of the books, many have dates which have not yet reached todays date, and others which have dates greater than todays date. Basically, all I want is the due date to appear bold (or strong), if it has passed todays date (the system displayed date). This is what I have and thought would work:

<?
if ($duedate < $date) {
  echo '<td><strong>';
} else {
  echo '<td>';
} ?>

<?php echo $date('d/m/Y', $timestamp);?></strong></td>

I have declared $timestamp as a var which converts the date of default MySQL format to a UK version. Can anyone help me out? I thought this would've been very straight forward!

Answers


try:

if (strtotime($duedate) < time()) {
    // oooh, your book is late!
}

Instead of working with the formatted dates, work with their timestamps. Either convert them back with strtotime() or use time() instead of date. Timestamps can be compared like regular numbers, because that's what they just are.


Okay :) Let's start here:

$date = date; // Wrong!
print $date("d/m/Y");

The above only works because PHP thinks date is a constant. But since you didnt set this constant PHP will convert it to the string 'date'. So $date contains 'date'. Then, when calling $date() as a function, PHP evaluates $date's content, which is 'date' and uses that as the function name, e.g. date(). What you really wanted to do was just $date = date('d/m/y').

Here is how date works:

string date  (  string $format  [,  int $timestamp  ] )

First argument is the desired output format, the second argument is an optional timestamp for which the output will be generated. If omitted it will be now. The function returns the output as string.

I assume your $duedates are already formatted strings, e.g. 2010-04-06. So when you do $duedate < $date, you are really doing a string comparison, because both variables hold formatted strings, but not timestamps.

Timestamps on the other hand are just numbers. A timestamp is the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). You can get the timestamp for the current date and time with the function time() and you can convert strings that represent dates with strtotime(). So when you want to compare your dates, do

if ( strtotime($duedate) < time() ) { // ... do something

And that's really all there is to it.


Need Your Help

How to Use View.isInEditMode() in custom views to skip code?

xml layout android-custom-view

I'm using a custom view to show gradient color filled TextView as bellow:

Why do numbers in a string become "x0n" when a backslash precedes them?

python string python-3.x

I was doing a few experiments with escape backslashes in the Python 3.4 shell and noticed something quite strange.