假设我想在27-10-2012
添加一周,以便我可以使用:
$week = 60*60*24*7;
$newdate = strtotime('27-10-2012') +$week;
或者我可以使用
$newdate = strtotime('+1 week',strtotime('27-10-2012'));
或者:
$date = new DateTime('2012-10-27');
$date->add(new DateInterval('P7D'));
所以有什么区别,特别是在使用周而不是monthes时?
referances: -
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php
和他们的功能签名。
答案 0 :(得分:1)
从纯代码的角度来看,它们都具有相同的值:
<?php
$week = 60*60*24*7;
$newdate = strtotime('27-10-2012') +$week;
echo $newdate;
echo '<br>';
$newdate = strtotime('+1 week',strtotime('27-10-2012'));
echo $newdate;
echo '<br>';
$date = new DateTime('2012-10-27');
$date->add(new DateInterval('P7D'));
echo strtotime($date->format('d-m-Y'));
?>
输出:
1351900800
1351900800
1351900800
从表现的角度来看,它有点不同。
方法1重复10.000次,取0.025587s
方法2重复10.000次,取0.12083s
方法3重复10.000次,取0.05738s
在每种情况下,它都是如此之小,您不必担心它。 现在,问问自己:哪一个更具可读性?
答案 1 :(得分:0)
性能可能会有所不同,但(除非代码处于大循环中),对您而言,开发人员最重要的区别是可读性。尝试从现在起1年后想象一下,当您更新代码时,您会看到:
$date->add(new DateInterval('P7D'));
那边的那条线说:在我的日期添加一个间隔,一个等于'P7D'的间隔...无论那意味着什么。
或者你可以看到:
$week = 60*60*24*7;
$newdate = strtotime('27-10-2012') +$week;
并且想想,好吧我在那个日期添加一个变量$ week。一周等于每分钟60秒,每小时60分钟,每天24小时和每周7天。是的,没错。
或者你可以看到:
$newdate = strtotime('+1 week',strtotime('27-10-2012'));
并说:$ newdate等于2012年10月27日之后的一周......下一个!
PHP本身就足够了,不要让他变得更加困惑。
关于性能,您可以确定这三者之间不会存在市长差异,尽管strtotime('+1 week'...
和new DateInterval('P7D')
需要进行一些文本解析,因此可能会有一些滴答。< / p>
答案 2 :(得分:0)
如果你在谈论表现明智/记忆用法,你的第一个例子可能是最直接和记忆意识。话虽如此,差异是MINOR,除非你循环浏览数千个结果并解析日期,否则你不会发现任何差异。
对象很棒,但它们确实使用了更多内存,因为它们具有更多功能。 DateTime
是一个很好用的对象,但在做一些简单的事情时,例如在某个日期添加一周,strtotime
就足够了。例如,你可以步行到1个街区外的商店或乘坐豪华轿车......如果你所做的只是增加一个星期,那就有点矫枉过正了。
话虽如此,当涉及到时区和不同地点的时间时,我会更频繁地使用DateTime
。在这个意义上工作得很好,绝对不会有点过分。
所有个人意见。
答案 3 :(得分:0)
处理周数很容易尝试一个月通常我会问自己这个问题..一个月内有多少天或几周?肯定会走60*60*24*Number of days
....的路线,因为这个月的每一天都没有固定,那么你可能会对你自己有一些不准确之处
简单的编程错误
$newdate = time() + (60 * 60 * 24 * 28);
^-----Now you have look for num of days
var_dump(date("Y-m-d", $newdate)); //string '2012-11-24' (length=10)
$newdate = strtotime('+1 month', strtotime('27-10-2012'));
var_dump(date("Y-m-d", $newdate)); //string '2012-11-27' (length=10)
$newdate = new DateTime('2012-10-27');
$newdate->add(new DateInterval('P1M'));
var_dump($newdate->format("Y-m-d")); //string '2012-11-27' (length=10)
从示例中,数字1选项已经出来
另一方面...... PHP DOC说
通过查看各个组件之间的分隔符来消除m / d / y或d-m-y格式的日期:如果分隔符是斜杠(/),则假设为美国m / d / y;而如果分隔符是破折号( - )或点(。),则假定为欧洲d-m-y格式。
为避免潜在的歧义,最好尽可能使用ISO 8601(YYYY-MM-DD)日期或DateTime :: createFromFormat()。
我敢打赌DateTime::createFromFormat()
是处理日期的最可靠方式,因为你100%
确定你在任何时间点传递的格式
答案 4 :(得分:0)
我对@pikzen(2012年)获得的结果感到有些惊讶,所以我使用strtotime()
对我自己进行了一些测试。我认为这个问题并没有从正确的角度思考。
如果我们要将strtotime()
与时间戳一起使用,则应在执行任何操作之前从开始转换时间戳。
E.G。我目前正在编写一种算法,可以在很长一段时间内创建重复的停电和假期,当然,还需要大量的日期验证和操作。
我使用PHP 7进行了一次1,000,000次重复测试,这是我的结果:
对于一个必须处理大量日期操作的复杂算法,在我看来,处理整数时间戳而不是字符串DateTimes有一点好处。
这是我的测试所用的代码:
set_time_limit(180); //3 minutes
echo "<style>table, tr, th, td {border-style:solid; padding:2px;}</style>" .
"<h1>Date add using strtotime() 1,000,000 repetitions</h1>" .
"<table><b><tr style='background-color:LavenderBlush;'><th>Unit</th>" .
"<th>String date with conversion</th><th>String date w/o conversion</th>" .
"<th>Timestamp with conversion</th><th>timestamp w/o conversion</th></tr></b>";
//add day
echo "<tr style='background-color:aqua;'><td><b>Day</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 day'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 day');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 day', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 day', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add week
echo "<tr style='background-color:LightCyan;'><td><b>Week</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 week'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 week');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 week', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 week', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add month
echo "<tr style='background-color:LightGreen;'><td><b>Month</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 month'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 month');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 month', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 month', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr>";
//add year
echo "<tr style='background-color:LightGoldenRodYellow;'><td><b>Year</b></td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = date('Y-m-d', strtotime($date . '+1 year'));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = date("Y-m-d");
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = strtotime($date . '+1 year');
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$newDate = date('Y-m-d', strtotime('+1 year', $date));
}
echo "<td>" . (microtime(true) - $test) . "</td>";
$date = strtotime(date("Y-m-d"));
$test = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$date = strtotime('+1 year', $date);
}
echo "<td>" . (microtime(true) - $test) . "</td><tr></table>";
//string date comparation
echo "<br /><h1>Date comparission with 1,000,000 repetition</h1>";
$date = date("Y-m-d");
$i = 0;
$newDate = date('Y-m-d', strtotime($date . '+1 year'));
$test = microtime(true);
while ($i < 1000000) {
if ($date !== $newDate) {
$i++;
}
}
echo "<br / ><b>String date comparission:</b> " . (microtime(true) - $test) . "<br />";
//timestamp comparision
$date = strtotime($date);
$i = 0;
$newDate = strtotime($newDate);
$test = microtime(true);
while ($i < 1000000) {
if ($date !== $newDate) {
$i++;
}
}
echo "<b>timestamp comparission:</b> " . (microtime(true) - $test) . "<br />";