简体   繁体   English

今天的strtotime

[英]strtotime of today

Hallo, I want to find the difference between today and another date, 你好,我想找到今天和另一个约会之间的区别,

convert todays date into unix time format here 将今天的日期转换为unix时间格式

 <?php
    echo '1st one'.strtotime(date("d.m.Y")).'<br>';
    echo '2nd one'.strtotime(date("m.d.Y")).'<br>';
    ?>

The first echo is producing some value, but not the second one. 第一个回声产生一些价值,但不是第二回声。 What is the bug in it...please help.. 它有什么错误...请帮忙..

strtotime makes assumptions based on the date format you give it. strtotime根据您提供的日期格式进行假设。 For instance 例如

date("Y-m-d", strtotime(date("d.m.Y"))) //=> "2010-09-27"
date("Y-m-d", strtotime(date("m.d.Y"))) //=> "1969-12-31"

Note that when given an invalid date, strtotime defaults to the timestamp for 1969-12-31 19:00:00, so when you end up with an unexpected date in 1969, you know you're working with an invalid date. 请注意,当给定无效日期时,strtotime默认为1969-12-31 19:00:00的时间戳,因此当您在1969年结束意外日期时,您知道您正在使用无效日期。

Because strtotime is looking for day.month.year when you use . 因为strtotime正在寻找day.month.year当你使用. as the delimiter, so it sees "9.27.2010" as the 9th day of the 27th month, which obviously doesn't exist. 作为分隔符,它将“9.27.2010”视为第27个月的第9天,显然不存在。

However, if you change it to use / as the delimiter: 但是,如果将其更改为使用/作为分隔符:

date("Y-m-d", strtotime(date("d/m/Y"))) //=> "1969-12-31"
date("Y-m-d", strtotime(date("m/d/Y"))) //=> "2010-09-27"

In this case, strtotime expects dates in month/day/year format. 在这种情况下, strtotime预计日期为月/日/年格式。

If you want to be safe, Ymd is generally a good format to use. 如果您想要安全,Ymd通常是一种很好的格式。

It's worth pointing out that strtotime() does accept words like "today" as valid input, so you don't need to put a call to date() in there if all you want is today's date. 值得指出的是, strtotime()确实接受像“今天”这样的单词作为有效输入,因此如果你想要的只是今天的日期,你就不需要在那里调用date()。 You could just use strtotime('today'); 你可以使用strtotime('today'); .

Come to think of it, a simple call to time(); 来想一想,简单地调用time(); will get you the current time stamp too. 会得到你当前的时间戳。

But to actually answer the question, you need to consider that dmY and mdY are ambiguous - if the day of the month is less than the 12th, it is impossible to tell which of those two date formats was intended. 但要真正回答这个问题,你需要考虑dmYmdY是不明确的 - 如果月份的日期小于12日,就不可能分辨出这两种日期格式中的哪一种是有意的。 Therefore PHP only accepts one of them (I believe it uses m/d/Y if you have slashes, but for dots or dashes it assumes dmY. 因此PHP只接受其中一个(我相信如果你有斜线,它使用m / d / Y,但对于点或破折号,它假定为dmY。

If you're using strtotime() internally for converting date formats, etc, there is almost certainly a better way to do it. 如果你在内部使用strtotime()转换日期格式等,几乎可以肯定有更好的方法。 But if you really need to do this, then use 'Ym-d' format, because it's much more universally reliable. 但是如果你真的需要这样做,那么使用'Ym-d'格式,因为它更加普遍可靠。

On the other hand, if you're accepting date input from your users and assuming that strtotime() will deal with anything thrown at it, then sadly you're wrong; 另一方面,如果您接受来自用户的日期输入并假设strtotime()将处理抛出的任何内容,那么遗憾的是您错了; strtotime() has some quite big limitations, of which you've found one. strtotime()有一些很大的限制,你已经找到了一个。 But there are a number of others. 但还有其他一些。 If you plan to use strtotime() for this sort of thing then you need to do additional processing as well. 如果您打算使用strtotime()来处理这类事情,那么您还需要进行额外的处理。 There may also be better options such as using a front-end Javascript date control to make it easier for your users without having to rely on strtotime() to work out what they meant. 可能还有更好的选择,例如使用前端Javascript日期控件使您的用户更容易,而不必依赖strtotime()来计算他们的意思。

strtotime would not take the dmy format. strtotime不会采用dmy格式。 good way is Ymd 好的方式是Ymd

strtotime does not consider 09.27.2010 to be a valid date... strtotime不认为09.27.2010是有效日期......

You could check it like this: 你可以这样检查:

<?php
// will return false (as defined by the docs)
echo var_dump(strtotime("09.27.2010"));
?>

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp. 该函数期望给出一个包含美国英语日期格式的字符串,并尝试将该格式解析为Unix时间戳。 US time format is : MM DD YYYY 美国时间格式为:MM DD YYYY

look here for the Information about which formats are valid http://www.php.net/manual/en/datetime.formats.php . 在这里查看有关哪些格式有效的信息http://www.php.net/manual/en/datetime.formats.php But what do you mean with deference between 2 dates? 但是,对于2个日期之间的尊重,你的意思是什么? You mean the Timespan between 2 dates? 你的意思是2个日期之间的Timespan?

echo (time() - strotime("- 2 days")) . " seconds difference";

Something like that? 那样的东西?

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

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