2013-06-08 20:25
浏览 301


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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dounabi6295
    dounabi6295 2013-06-08 20:56

    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.

    点赞 评论
  • dongxiong1935
    dongxiong1935 2013-06-08 20:39

    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);

    If you wish to do it directly in MySQL.

    Date and time:


    Just date:

    点赞 评论
  • dtu1747
    dtu1747 2013-06-08 20:55

    I think is what you are looking for:

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

    点赞 评论