tags:

views:

122

answers:

5

Hi..

Is there any script to check the current date in PHP?

I want to use this function to display a date from mysql db, and if the date from the db is the same as todays date, then display "TODAY" instead...

Thanks...

+4  A: 

How about using the date function

jitter
+4  A: 

To get the current timestamp (seconds since 1970), use time()

To convert that into pretty much any format you want, use date()

To compare, there's a number of ways you could do it, but I think the simplest would be this:

$dateFromDB = getTheDateFromMyDB();   // "2009-10-20"
$today = date("Y-m-d");

if ($today == $dateFromDB) {
    // ...
}
nickf
A: 

run a SQL query something like:

$sql = 'SELECT * FROM TABLE WHERE entryDate = "'.date('m/d/Y').'";';

You will then be checking for todays date, get more info on the date() fn here: http://us3.php.net/manual/en/function.date.php

Jakub
A: 

Just thought I'd point out that MySQL has a function for this:

select curdate() as todays_date

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

genio
A: 

Do something like this:

$result = mysql_query("select CURDATE()");

$mysqlDate = mysql_result($result, 0);

The above will give you the date in 'yyyy-mm-dd' format, then you need to get the date from PHP.

$phpDate = date("Y-m-d");

This gives the same format as above.

You can then do something like this

if ( $phpDate == $mysqlDate )
      $dateToDisplay = 'Today';
else
    $dateToDisplay = $mysqlDate;

echo $dateToDisplay;

This assumes of course that the date.timezone has been set in the ini file or you can call

date_default_timezone_set() to avoid warnings in Strict mode.

Steve Obbayi