开发者

get current date with PHP help

开发者 https://www.devze.com 2022-12-08 17:29 出处:网络
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

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


How about using the date function


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消