tags:

views:

53

answers:

2

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!

A: 

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.

Gordon
I do not have '()' declared anywhere for a date? Why do I need this?Can you also explain further what you mean by convetting them back? Can't I simly compare the dates with the new formatting? I have a feature that sorts my dates in order, and this recognises them fully (by correctly sorting)
Yvonne
+1  A: 

try:

if (strtotime($duedate) < time()) {
    // oooh, your book is late!
}
webbiedave
I'm getting an unexpected '{' for some reason with this?
Yvonne
Added another closing bracket at the end,so appears like this 'time())) {' ...The output is fine in terms of the information, but still no recognition of an overdue book :(
Yvonne
Hey I managed to do this, I set the $timestamp varaible as (strtotime($duedate), then used if ($timestamp < time()) { // oooh, your book is late! } :D thanks for help anyway
Yvonne
You're welcome.
webbiedave
This will work, but be careful time() returns unix timestamp independantly of any timezone, and strtotime can parse TZ data, especially if you have used date_default_timezone_set before
Benoit