dorpbn1027 2013-06-08 20:25
浏览 1435
已采纳

如何从MySQL时间戳数据类型的日期中减去一天?

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?