Why is php strtotime() not working on a date? - php

This is the code I have used. Why can't it understand this date?
strtotime('FRI OCT 14TH 2016'); //returns Thursday 01 01 1970

Try this:
echo date('d-m-Y', strtotime('FRI OCT 14 2016')); // remove TH from 14TH
it will return
14-10-2016

As Neat mentioned in a comment:
Because thats not a supported format, Date Formats.
Looking at the PHP documentation for Date Formats, the first row in the table describes the suffixes supported on dates (see the screenshot below). Notice that the letters are all lower-case.
.
Thus as was mentioned in comments, use lower-case letters for the suffixes.
strtotime('FRI OCT 14th 2016');
See a demonstration of this change in this playground example.

Related

Strtotime won't convert my date accurately

I know this is a common problem, but I can't seem to find the solution anywhere. In my last question [complicated date functions - comparing, subtracting I needed to compare timestamps to get an accurate date for some Cisco logs.
The best I can come up with (since the dates don't actually feature the year) is in the format
Mar 1 2013 00:03:55:
from
Mar 1 00:03:55:
But when I ran some tests, strtotime is converting this date as
Jan 1970
Using
print date("M Y", strtotime($c_log))."\n";
Am I going to have to reformat it into a date it can understand? I don't appear to have the DateTime function. What's the simplest way?
Use the date_parse_from_format() function so you can specify the format
http://php.net/manual/en/function.date-parse-from-format.php
Works for me:
echo date('M Y', strtotime('Mar 1 00:03:55')); // outputs Mar 2013
it should default to the current year when one isn't included.

Convert a date in php, toString function isn't working

I'm trying to convert a date to a string by using the toString('dd MMMM YYY') function.
But here, this isn't working.
Here is my code:
$date_start = new Zend_Date(strtotime($this->startdate));
echo($date_start);
The result is: 31 Dec 2012 00:00:00
$date_input = $date_start->toString('dd MMMM YYY');
echo($date_input);
The result is: 31 December 2013
What do I have to do to obtain 31 December 2012 ?
This is a known issue described here: http://framework.zend.com/issues/browse/ZF-5297
Note that the default ISO format differs from PHP's format which can be irritating if you have not used in previous. Especially the format specifiers for Year and Minute are often not used in the intended way.
For year there are two specifiers available which are often mistaken. The Y specifier for the ISO year and the y specifier for the real year. The difference is small but significant. Y calculates the ISO year, which is often used for calendar formats. See for example the 31. December 2007. The real year is 2007, but it is the first day of the first week in the week 1 of the year 2008. So, if you are using 'dd.MM.yyyy' you will get '31.December.2007' but if you use 'dd.MM.YYYY' you will get '31.December.2008'. As you see this is no bug but a expected behaviour depending on the used specifiers.
Use "yyy" instead of "YYY".
Use yyy instead of YYY.
YYY is the ISO-8601 date, which is different from a calendar date.
http://framework.zend.com/manual/1.12/en/zend.date.constants.html

strtotime fails on pubDates from RSS feeds?

Example:
print strtotime('Sat, 03 Nov 2012 20:17:12 0000');
prints nothing :(
And the date string appears to be correct..
it's:
day name, day month name year hour:min:sec timezone
Valid formats are explained in Date and Time Formats.
Consider using DateTime objects, and the createFromFormat() method
I get it you want to add a timezone correction. But the timezone requires a sign.
print strtotime('Sat, 03 Nov 2012 20:17:12 +0000');
This will work
print strtotime('03 Nov 2012 20:17:12');
Output: 1351988232
This works perfectly. You should remove the 0000 and the day (Sat)

PHP strtotime for June returns July

I'm stumped as to why the following PHP strtotime function returns '07' as the month number, rather than '06' when $monthToGet = 'June':
$monthToGet = $_GET['mon'];
$monthAsNumber = date('m', strtotime($monthToGet));
From searching, it appears it may be due to default date parameters (in this case the day and year) as I haven't specified them. Would that be the cause?
Any suggestions appreciated!
TL;DR
You are right
echo date("m", strtotime("June"));
-> 07
However, this does work:
echo date("m", strtotime("1. June 2012"));
-> 06
The problem explained
Today is 31. July 2012 and since you provide only a month, the current day and current year are used to create a valid date.
See the documentation:
NOTE
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
Alternatives
You could use date_parse_from_format() or strptime() to achieve what you want with a slightly different approach.
(Thanks to johannes_ and johann__ for their input)
Fixed with :
$monthToGet = '1 '. $_GET['mon'];
But I still don't get why, since "m" is a valid date format
Today is 31 Jul. So a strtotime with only "June" is interpreted as 31 June => 1 July.
In fact:
echo date("Y-m-d",strtotime("January")); // 2012-01-31
echo date("Y-m-d",strtotime("February")); // 2012-03-02
of course... only today 31 Jul 2012 :) Tomorrow all will works.
You're lucky because you found this bug just today ;)

Change format of date with PHP

I want to change this date format: "Tue Apr 3 15:00:03 GMT+0300 2012" to "3.4.2012" with PHP. Is it possible?
I tried:
$date="Tue Apr 3 15:00:03 GMT+0300 2012";
echo date('d.m.Y', strtotime($date));
but it results in: 03.04.2015. What am I doing wrong?
Read the Manual before using a function, and especially before asking other people to read it for you, like I just did to answer your question:
date('j.n.Y', strtotime($date));
Codepad Example
Your date is not formatted correctly, at least it is not the RFC 2822 date format,
Tue, 3 Apr 2012 15:00:03 +0300
strtotime is quite flexible but it cannot guess what you mean ...

Categories