Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

php

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!

like image 321
Yvonne Avatar asked Apr 06 '10 15:04

Yvonne


People also ask

How do you check if a date is before another date?

To check if a date is after another date, compare the Date objects, e.g. date1 > date2 . If the comparison returns true , then the first date is after the second, otherwise the first date is equal to or comes before the second. Copied!

How do I validate a date today?

To check if a date is today's date:Use the toDateString() method to compare the two dates. If the method returns 2 equal strings, the date is today's date.

How do you know if a date was yesterday?

To check if a date is yesterday:Subtract 1 day from the current date to get yesterday's date.


1 Answers

try:

if (strtotime($duedate) < time()) {
    // oooh, your book is late!
}
like image 56
webbiedave Avatar answered Oct 16 '22 07:10

webbiedave