简体   繁体   English

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

[英]How to substract one day from a date in MySQL timestamp data type?

In my Worpdress site I am using the function current time to return the current date in MySQL timestamp data type format. 在我的Worpdress网站上,我正在使用函数current time以MySQL timestamp数据类型格式返回当前日期。

My goal is to perfom some operations on the result (like substracting 1 day). 我的目标是对结果执行一些操作(例如减去1天)。 I found on the web date_sub but the requirement is that its parameter should be a DateTime type. 我在网上找到了date_sub但要求是它的参数应该是DateTime类型。

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 警告:date_sub()期望参数1为DateTime,第107行的C:\\ xampp \\ htdocs \\ blog \\ wp-content \\ themes \\ twentyten \\ header.php中给出的字符串

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? 如何在MySQL时间戳数据类型格式上执行减法,或者有Worpdress函数以DataTime格式返回当前日期?

You help is always appreciated. 您的帮助总是值得赞赏的。

PHP date function can return date in whatever format you want. PHP date函数可以以您想要的任何格式返回日期。

$datetime = date('Y-m-d H:i:s', time() - (1 * 86400));

This will generate curent time in unix and take one day out. 这将在Unix中产生当前时间,并需要一天的时间。 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 http://php.net/manual/zh/function.date.php

If you wish to do it directly in MySQL. 如果您希望直接在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: 如果您需要在17天前打印出日期,现在只需发出以下电话:

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). 这将使当前的Unix时间戳减去17天前以来的秒数(17天×24小时×60分钟×60秒)。

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. 如果您不熟悉它,则应查看PHP SPL日期/时间类。 They are immensely useful and make this type of thing very easy. 它们非常有用,使这种事情非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM