strtotime of today - php

Hallo, I want to find the difference between today and another date,
convert todays date into unix time format here
<?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. 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.
Because strtotime is looking for day.month.year when you use . as the delimiter, so it sees "9.27.2010" as the 9th day of the 27th month, which obviously doesn't exist.
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.
If you want to be safe, Y-m-d is generally a good format to use.

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. You could just use strtotime('today');.
Come to think of it, a simple call to time(); will get you the current time stamp too.
But to actually answer the question, you need to consider that d.m.Y and m.d.Y 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. 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 d-m-Y.
If you're using strtotime() internally for converting date formats, etc, there is almost certainly a better way to do it. But if you really need to do this, then use 'Y-m-d' format, because it's much more universally reliable.
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() has some quite big limitations, of which you've found one. 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. 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.

strtotime does not consider 09.27.2010 to be a valid date...
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. US time format is : MM DD YYYY

look here for the Information about which formats are valid http://www.php.net/manual/en/datetime.formats.php. But what do you mean with deference between 2 dates? You mean the Timespan between 2 dates?
echo (time() - strotime("- 2 days")) . " seconds difference";
Something like that?

strtotime would not take the d.m.y format. good way is Y-m-d

Related

False dateresult using date() php

I want to parse the date 1938 1938+02:00 using date() & strtotime().
My code:
echo date("Y", strtotime("1938+02:00"));
gives me as result "2014"..
What am i doing wrong?
For something like this just get the first four characters of the string. No need to work with dates and such:
echo substr('1938+02:00', 0, 4);
Demo
But if you insist on using date functionality you'll need to use DateTime::createFromFormat() as that date string is not a standard format.
$date = DateTime::createFromFormat('YP', '1938+02:00');
echo $date->format('Y');
Demo
date("Y"); only return the year. That's what the Y does.
See the Manual page for date for other options.
EDIT
Another thing to consider, is that timestamps only go back to 1970. That's what a timestamp is (the number of seconds since 1970).
So, that's going to give you a negative value for the timestamp.
Your date string is not in an acceptable format. here is a list of acceptable formats for strtotime

How to format date in PHP from a string of concatenated numbers?

I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.

Human readable date into timestamp

I am trying to parse a logfile, using human readable dates without a year into a timestamp. I have looked over the function strtotime() but haven't had any success with it.
Example time: Apr-26-10:49:36
which is the equiv of "M-j-H:i:s" for the date() function.
Edit: without a year, in my case here..its perfectly fine to assume the year is the current year.
I've created a script to break this down in the past, but it was long and redundant. I was hoping for a more simplified way of doing this. Any help, or pointing in the right direction would be greatly appreciated :D
If you know the format of the date, you can use strptime to parse it. This returns an array that you can use to determine the arguments for mktime.
Maybe not the prettiest way to do it but it works
The problem why the strtotime function is not working is because the hyphen between the Day and the Hour 26-10.
You can replace the - and then use strtotime.
echo date('Y-m-d H:i:s', strtotime(str_replace('-', ' ', 'Apr-26-10:49:36')));

How to format datetime most easily in PHP?

To change 2009-12-09 13:32:15 to 09/12/2009
here:
echo date("d/m/Y", strtotime('2009-12-09 13:32:15'))
You can use strtotime to get the timestamp of the first date, and date to convert it to a string using the format you want.
$timestamp = strtotime('2009-12-09 13:32:15');
echo date('d/m/Y', $timestamp);
And you'll get :
09/12/2009
[edit 2012-05-19] Note that strtotime() suffers a couple of possibly important limitations:
The format of the date must be YYYY-MM-DD; it might work in some other cases, but not always !
Also, working with UNIX Timestamps, as done with date() and strtotime() means you'll only be able to work with dates between 1970 and 2038 (possibly a wider range, depending on your system -- but not and illimited one anyway)
Working with the DateTime class is often a far better alternative:
You can use either DateTime::__construct() or DateTime::createFromFormat() to create a DateTime object -- the second one is only available with PHP >= 5.3, but allows you to specify the date's format, which can prove useful,
And you can use the DateTime::format() method to convert that object to any date format you might want to work with.
Using the date() method.
print date("d/m/Y", strtotime("2009-12-09 13:32:15"));
$long_date = '2009-12-09 13:32:15';
$epoch_date = strtotime($long_date);
$short_date = date('m/d/Y', $epoch_date);
The above is not the shortest way of doing it, but having the long date as an epoch timestamp ensures that you can reuse the original long date to get other date format outputs, like if you wanted to go back and have just the time somewhere else.

formatting dates with or without a leading zero in php

How would you convert a datestring that will be formated like...
m/d/yyyy H:i:s or mm/dd/yyyy H:i:s...
and format it like... yyyy-mm-dd H:i:s
I can format either of the two inputs into my desired format, but not both.
function format_date($date){
return date('Y-m-d H:i:s',strtotime($date));
}
should do the trick.
Easiest way would be to simply transform it to unix time first with strtotime then run strftime on it afterwards... not the bes practice but it eliminates alot of the potential format parsing issues :-)
How are you formatting it now? If you're certain that those are the only two date formats you'll possibly have as input, then you can explode() it on the forward slashes, then if strlen() returns 1 on the month or the date, add a 0 as you're creating your yyyy-mm-dd string.
However, if you're not guaranteed that those are your only two input formats, then I would use strtotime() to convert it to epoch, then use date() to format it as you desire. A slightly bigger processing hit, but much more universal code.

Categories