[英]PHP: Update a date value?
What is the easiest way to update a date? 更新日期最简单的方法是什么?
In my database I have a field called mydate that is setup as a timestamp datatype. 在我的数据库中,有一个名为mydate的字段,该字段设置为时间戳记数据类型。 (It as to be this datatype). (作为此数据类型)。
My dates are listed as either: 我的约会被列出为:
2013-07-25 00:00:00
or
0000-00-00 00:00:00
Meaning they either have a data or just zeroes, but never empty. 意味着它们要么有数据,要么只有零,但永远不会为空。
Edit: Thank you all! 编辑:谢谢大家! I sort of combined all that you said and made it work. 我将您所说的全部结合起来并使其起作用。
Now lets say I wanted to increase any give date with 10 days how would I accomplish that? 现在让我说,我想将任何给定日期增加10天,我该如何完成?
The first one, July 25th 2013 would become 2013-08-04 00:00:00 and the other would become 2013-06-18 00:00:00 (at the time of posting it's June the 8th). 第一个是2013年7月25日,变成2013-08-04 00:00:00,另一个是2013-06-18 00:00:00(在6月8日发布时)。
How to? 如何?
Something along these lines: 遵循以下原则:
if(mydate_from_db > present_date){
mydate_from_db = mydate_from_db + day('10')
}
else{
mydate_from_db = present_date + day('10')
}
Update mysql with mydate where relevant. 在相关的地方使用mydate更新mysql。
With PHP you can do something like: 使用PHP,您可以执行以下操作:
if($mydate_from_db > $present_date){
$mydate_from_db = strtotime($mydate_from_db, '+ 10 days');
} else {
$mydate_from_db = strtotime($present_date, '+ 10 days');
}
Or you can do it with mysql like: 或者,您也可以使用mysql这样的方法:
UPDATE yourtable SET mydate = IF(mydate = '0000-00-00 00:00:00', '2013-06-18 00:00:00', DATE_ADD(mydate, INTERVAL 10 DAY));
You can do it in MySql directly: 您可以直接在MySql中执行此操作:
UPDATE
my_table
SET
my_date = my_date + INTERVAL 10 DAY
WHERE
DATE(my_date) >= '2013-06-01'
if(mydate_from_db > present_date)
{
$date = strtotime(mydate_from_db );
$date = strtotime("+10 day", $date);
echo date('M d, Y', $date);
}
else
{
$date=date();
$date = strtotime("+10 day", $date);
echo date('M d, Y', $date);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.