In my Worpdress site I am using the function current time to return the current date in MySQL timestamp data type format.
My goal is to perfom some operations on the result (like substracting 1 day). I found on the web date_sub
but the requirement is that its parameter should be a DateTime
type.
Warning: date_sub() expects parameter 1 to be DateTime, string given in C:\xampp\htdocs\blog\wp-content\themes\twentyten\header.php on line 107
How can I perform substraction on the MySQL timestamp data type format, or is there a Worpdress function that returns current date in DataTime
format?
You help is always appreciated.
Something like this should work:
$date = new DateTime();
$date->sub(new DateInterval("P1D"));
echo $date->format("Y-m-d H:i:s");
If you aren't familiar with it, you should look at PHP SPL Date/Time classes. They are immensely useful and make this type of thing very easy.
PHP date function can return date in whatever format you want.
$datetime = date('Y-m-d H:i:s', time() - (1 * 86400));
This will generate curent time in unix and take one day out. If fed as second param to date function it will be converted in that format.
$a_day_ago = time() - (1 * 86400);
http://php.net/manual/en/function.date.php
If you wish to do it directly in MySQL.
Date and time:
NOW() - INTERVAL 1 DAY
Just date:
CURDATE() - INTERVAL 1 DAY
I think is what you are looking for:
<?php
function longdate($timestamp)
{
return date("l F jS Y", $timestamp);
}
?>
If you need to print out the date 17 days ago, you now just have to issue the following call:
echo longdate(time() - 17 * 24 * 60 * 60);
which passes to longdate the current Unix timestamp less the number of seconds since 17 days ago (17 days × 24 hours × 60 minutes × 60 seconds).