user188962
user188962

Reputation:

get current date with PHP help

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...

Upvotes: 0

Views: 339

Answers (5)

Steve Obbayi
Steve Obbayi

Reputation: 6085

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.

Upvotes: 0

genio
genio

Reputation: 882

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

Upvotes: 0

Jakub
Jakub

Reputation: 20475

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: https://www.php.net/manual/en/function.date.php

Upvotes: 0

nickf
nickf

Reputation: 545985

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) {
    // ...
}

Upvotes: 3

jitter
jitter

Reputation: 54605

How about using the date function

Upvotes: 4

Related Questions