Php 生成并存储今日日期+;11个月和+;一年
我需要用php生成一些日期,然后将这些日期存储在MySQL数据库中 今天日期,今天日期+1年,今天日期+11个月,今天日期+364天 ie:2013年9月12日、2014年9月12日、2014年8月12日、2014年9月11日 最好的办法是什么 我可以从以下地点获得今天的日期:Php 生成并存储今日日期+;11个月和+;一年,php,mysql,date,Php,Mysql,Date,我需要用php生成一些日期,然后将这些日期存储在MySQL数据库中 今天日期,今天日期+1年,今天日期+11个月,今天日期+364天 ie:2013年9月12日、2014年9月12日、2014年8月12日、2014年9月11日 最好的办法是什么 我可以从以下地点获得今天的日期: function zen_date_created_stat($raw_date) { if ($raw_date == '') return false; $year = substr($raw_dat
function zen_date_created_stat($raw_date) {
if ($raw_date == '') return false;
$year = substr($raw_date, 2, 2);
$month = (int)substr($raw_date, 4, 2);
$day = (int)substr($raw_date, 6, 2);
return date(DATE_FORMAT, mktime(0, 0, 0, $month, $day, $year));
}
$date_installed = date("d M y");
到目前为止,我没有得到其他日期,只是有一大堆错误。请看:。从示例中可以看出:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
看看:。从示例中可以看出:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
在PHP中修改日期的两种常见方法:
- 与…一起使用
- 使用类及其or方法
注意:这个答案是为了提高认识和RTM而特意概括的。在PHP中修改日期的两种常见方法:
- 与…一起使用
- 使用类及其or方法
$today = strtotime("now");
$next_year = strtotime("+1 year");
$eleven_months = strtotime("+11 months");
$many_days = strtotime("+364 days");
// personally I'd recommend "+1 year -1 day" to account for leap years
这可能是你最好的选择:
$today = strtotime("now");
$next_year = strtotime("+1 year");
$eleven_months = strtotime("+11 months");
$many_days = strtotime("+364 days");
// personally I'd recommend "+1 year -1 day" to account for leap years
如果是我,我会用
strotime()
接受两个参数。第一个参数是一个字符串,指示日期或操作(例如+11个月)。第二个是可选的,指示要在其上执行操作的基本时间戳。如果不提供第二个参数,函数将使用NOW作为默认时间
<?php
$todays_date = strtotime("today");
$tomorrows_date = strtotime("+1 day");
$delta_eleven = strtotime("+11 months");
$delta_year = strtotime("+1 year");
$delta_364 = strtotime("+364 days");
//the values you have above are TIMESTAMPS (the number of seconds since January 1 1970 00:00:00 UTC)
//if you want dates, you can convert as follows
$today = date("m/d/Y", $todays_date);
$tomorrow = date("m/d/Y", $tomorrows_date);
$plus_eleven = date("m/d/Y", $delta_eleven);
$plus_year = date("m/d/Y", $delta_year);
$plus_364 = date("m/d/Y", $delta_364);
echo $today . " " . $tomorrow . " " . $plus_eleven . " " . $plus_year . " " . $plus_364;
?>
如果是我,我会用
strotime()
接受两个参数。第一个参数是一个字符串,指示日期或操作(例如+11个月)。第二个是可选的,指示要在其上执行操作的基本时间戳。如果不提供第二个参数,函数将使用NOW作为默认时间
<?php
$todays_date = strtotime("today");
$tomorrows_date = strtotime("+1 day");
$delta_eleven = strtotime("+11 months");
$delta_year = strtotime("+1 year");
$delta_364 = strtotime("+364 days");
//the values you have above are TIMESTAMPS (the number of seconds since January 1 1970 00:00:00 UTC)
//if you want dates, you can convert as follows
$today = date("m/d/Y", $todays_date);
$tomorrow = date("m/d/Y", $tomorrows_date);
$plus_eleven = date("m/d/Y", $delta_eleven);
$plus_year = date("m/d/Y", $delta_year);
$plus_364 = date("m/d/Y", $delta_364);
echo $today . " " . $tomorrow . " " . $plus_eleven . " " . $plus_year . " " . $plus_364;
?>
-1提供的变量是UNIX时间戳(自1970年1月1日00:00:00 UTC以来的秒数),而不是日期。要将时间戳转换为日期,请使用date函数。(例如date(“m/d/Y”,今天$)@Lumberjack考虑到OP已经在使用date
转换mktime
给出的时间戳,我有点假设他们知道如何在strotime
上使用它。@Kolink:这不是最好的选择,最好的选择是使用DateTime
-1提供的变量是UNIX时间戳(自1970年1月1日00:00:00 UTC以来的秒数)不是日期。若要从时间戳转换为日期,请使用日期函数。(例如日期(“m/d/Y”,$today);)@伐木工人考虑OP已经在使用date
转换mktime
给出的时间戳,我有点假设他们知道如何在strotime
上使用它。…@Kolink:这不是最好的选择,最好的选择是使用DateTime
-提供的1个变量是UNIX时间戳(自1970年1月1日00:00:00 UTC以来的秒数)不是日期。若要从时间戳转换为日期,请使用日期函数。(例如日期(“m/d/Y”,$today);)@伐木工人——至少可以这么说,这太迂腐了。OP显然掌握了如何将所需的值转换为他可以使用的有效格式,他只需要知道如何首先获取数据,而大多数答案都提供了解决方案。@Glavić——毫无疑问,在我看来,你知道答案,所以我会建议如果你没有答案,不要乱翻。这里只有一个乱翻的是你们,不会以重复的形式结束这个问题…-1提供的变量是UNIX时间戳(1970年1月1日00:00:00 UTC以来的秒数),而不是日期。要从时间戳转换为日期,请使用date函数。(例如date(“m/d/Y”,$today);)@伐木工人——至少可以这么说,这太迂腐了。OP显然掌握了如何将所需的值转换为他可以使用的有效格式,他只需要知道如何首先获取数据,而大多数答案都提供了解决方案。@Glavić——毫无疑问,在我看来,你知道答案,所以我会建议如果你没有答案,不要乱翻。这里唯一乱翻的是你们,不要把这个问题当作重复的问题来结束…我建议在你的查询中做日期算术。在MySQL手册中查找“日期算术”不要在PHP中弄乱它。最好的方法是什么?
简单。使用。我建议在查询中使用日期算术。查看MySQL手册中的“日期算术”不要在PHP中弄乱它。做这件事的最佳方法是什么?
简单。使用。你已经意识到你已经把其他人的答案弄乱了,只留下了相同的答案(使用date()
)在其他人回答后16分钟。@Jason McCreary-OP要求的是一个日期,而不是时间戳。我指出其他答案的缺点并不是为了从其他回答的人那里得到情感上的反应,而是为了促进一个准确完整的答案。如果我这样做让你不高兴,我道歉。@Glavic integer的结果来自+60年超过整数的最大值。请尝试“echo strottime”(+24年);echo strottime”(+25年)5942最好是因为它需要更少的按键来编写代码。但那只是因为我可怜地试图赢得与一个完全陌生的人的毫无意义的争论。相反,我会说实话:我说这是“最好的”,因为这是我最熟悉的答案。我对DateTime(个人)不太满意我发现strotime()简单易懂,而即使在阅读了DateTime:Add文档之后,我仍然需要更多的信息才能回答Op。你已经意识到,你只不过在阅读了其他人的答案之后