Strtotime today

Hallo, I want to find the difference between today's and another date,

convert today's date to unix format here

<?php echo '1st one'.strtotime(date("dmY")).'<br>'; echo '2nd one'.strtotime(date("mdY")).'<br>'; ?> 

The first echo produces some value, but not the second. What a mistake in it ... please help ..

+4
source share
6 answers

strtotime makes assumptions based on the date format you give it. For instance,

 date("Ymd", strtotime(date("dmY"))) //=> "2010-09-27" date("Ymd", strtotime(date("mdY"))) //=> "1969-12-31" 

Please note that for a given 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 that you are working with an invalid date.

Because strtotime looking for day.month.year when you use . as a separator, so he sees "9.27.2010" as the 9th day of the 27th month, which obviously does not exist.

However, if you change it to use / as a separator:

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

In this case, strtotime expects dates in the format month / day / year.

If you want to be safe, Ymd is usually a good format.

+8
source

It's worth noting that strtotime() accepts words like "today" as a valid input, so you don't need to put a call on date () if all you need is today's date. You can simply use strtotime('today'); .

Think about it, a simple call to time(); will also give you the current timestamp.

But in order to answer this question, you need to consider that dmY and mdY ambiguous - if the day of the month is less than the 12th, it is impossible to determine which of these two date formats was intended, therefore PHP accepts only one of them (I believe that it uses m / d / Y if you have slashes, but for dots or dashes it assumes dmY.

If you use strtotime () to convert date formats, etc., there is almost certainly the best way to do this. But if you really need to do this, use the 'Ym-d' format because it is much more versatile.

On the other hand, if you accept data entry from your users and think that strtotime () will deal with anything that would be thrown at it, then, unfortunately, you are mistaken; strtotime () has some pretty big limitations from which you found it. But there are others. If you plan on using strtotime () for this kind of thing, you also need to do extra processing. There may also be more efficient options, such as using the front-end Javascript control to make it easier for your users without relying on strtotime () to figure out what they mean.

+3
source

strtotime does not consider 09.27.2010 valid date ...

You can check it as follows:

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

The function expects a string containing the date format in English in the USA to be set and will try to parse this format in the Unix timestamp. US Data Format: MM DD YYYY

0
source

see here for information on which formats are valid http://www.php.net/manual/en/datetime.formats.php . But what do you mean with respect between the two dates? Do you mean Timespan between two dates?

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

Something like that?

0
source

strtotime does not accept dmy format a good way is ymd

0
source

All Articles