【问题标题】:Generate and store todays date + 11 months, and + 1 year生成并存储今天的日期 + 11 个月和 + 1 年
【发布时间】:2013-09-12 15:42:47
【问题描述】:

我需要在 php 中生成一些日期,然后将它们存储在 MySQL 数据库中。

今天日期,今天日期 + 1 年,今天日期 +11 个月,今天日期 + 364 天

即: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_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");

到目前为止,我还没有得到其他日期,只是一堆错误。

【问题讨论】:

  • 我建议在您的查询中进行日期运算。在 MySQL 手册中查找“日期算术”,而不是在 PHP 中弄乱它。
  • What is the best way to go about this? 简单。使用DateTime

标签: php mysql date


【解决方案1】:

看看:strtotime。从例子:

<?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";
?>

【讨论】:

  • -1 提供的变量是 UNIX 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数)而不是日期。要将时间戳转换为日期,请使用 DATE 函数。 (例如日期(“m/d/Y”,$today);)
  • @Lumberjack - 这至少可以说是迂腐的。 OP 显然掌握了如何将所需值转换为他可以使用的有效格式,他只需要首先知道如何获取大多数答案都提供了解决方案的数据。
  • @Glavić - 毫无疑问,您知道答案,所以我建议如果您没有答案,请不要拖钓。
  • 只有一个在这里拖钓的是你们,不要将这个问题作为重复来关闭......
【解决方案2】:

strtotime 可能是你最好的选择:

$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

【讨论】:

  • -1 提供的变量是 UNIX 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数)而不是日期。要将时间戳转换为日期,请使用 DATE 函数。 (例如日期(“m/d/Y”,$today);)
  • @Lumberjack 考虑到 OP 已经在使用date 来转换mktime 给出的时间戳,我有点假设他们知道如何在strtotime 上使用它...
  • @Kolink:这不是最好的选择,最好的选择是使用DateTime
【解决方案3】:

PHP中修改日期的两种常用方法:

注意:这个答案是故意笼统的,以提高知名度和 RTM。

【讨论】:

  • +1 Note: This answer is intentionally generally to promote awareness and RTFM. 我想我们都一样;)
  • @webnoob,必须为拖钓选民添加免责声明。 :) 确实,我一直看到有关日期操纵的问题。迟早,我会做出一个答案来统治他们。
【解决方案4】:

如果是我,我会使用strtotime()

strtotime() 接受两个参数。第一个参数是一个字符串,表示日期或操作(例如 +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;
?>

【讨论】:

  • 您确实意识到您在其他人的回答中拖了后腿,结果却在其他人之后 16 分钟留下同一个(使用date())。
  • @Jason McCreary - OP 要求提供日期,而不是时间戳。我并没有指出其他答案的缺点,以引起其他回答者的情绪反应,而是为了促进准确和完整的答案。如果我这样做让你不高兴,我很抱歉。
  • @Glavic +60 年的整数结果超过了整数的最大值。试试 ` echo strtotime("+ 24 years"); echo strtotime("+25 years");` 第一个会起作用,第二个不会。
  • OP 没有要求加 60 年,他想加 1 年,但我明白你的意思。然而,有人可能会争辩说,并非所有应用程序都需要在编写时考虑到 35 年的生命周期。
  • 我可以说If as you say the two answers are identical, then strtotime() is best because it requires less keystrokes to code.,但这只是因为我可怜地试图赢得一个完全陌生的毫无意义的争论。相反,我会说实话:我说它是“最好的”,因为这是我最熟悉的答案。我对 DateTime(个人)不像对 strtotime() 那样舒服。我发现 strtotime() 简单易懂,而即使在阅读了 DateTime:Add 文档之后,我仍然需要更多信息才能回答 OP
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多