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
Related
I'm using a time-based token using as seed the year and the number of week, example:
$token = md5($salt + gmdate('YW'));
Yesterday sunday the token got broken, the time on server and client was synced (on time).
Checking with the interactive cli tool of php, I realized the number of week returned by PHP is "01" (zero-one).
ex:
Interactive mode enabled
php > echo gmdate('W');
01php > echo date('W');
01php >
At the moment of this post, it is 29-dec at 21:58 UTC (9:58 pm) and this was broken since yesterday at 00:00 UTC.
Tested on php 5.5.9
Instead of format character Y, use o, which will return ISO-8601 year number.
This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
Example:
echo gmdate('oW');
Probably due to different timezone or locale settings. In some countries/standards sunday is considered as the first day of the week.
WEEK(date[mode]);
date = a date value. mode = An integer indicating the starting of
the week.
The default arugment is 0 which is sunday, setting this to 1 will be
monday, 2 tuesday and so on.
week(date,1);
Quoted from How do I set first day of the week to Monday when using Week(Date) in PHP/MySQL?
I found the problem.
In the ISO-8601 standar, this week is the 01 of 2015, and not the last of 2014. This can be check here:
The problem is, I on the client use a function returns 2015 and 1 (And I from PHP use 2014 and 01 "201401" !== "20151").
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.
I have a date in oW format. This is the 4 digit year in ISO 8601 format followed by the week number. As an example, 201301 represents the week starting on Monday, Dec. 31, 2012.
How can I convert this back to a timestamp. Going from timestamp to string works with date('oW',$ts). How do I go backwards? I am interested in getting the Monday of the week represented in oW format. It would be very silly if PHP provided a way to go in one direction ut did not bother with the inverse.
ISO 8601 expects week dates in the following format:
YYYY\WWW
For example:
2012W52
is the start of the 52st week in 2012 what is actually the 2012/12/24.
To convert week dates back into a timestamp you can use the php function strtotime(). The code will look like follows:
$oW = '2012W53';
$time = strtotime($oW);
// will output: 2012-12-31T00:00:00+01:00 (I'm in CEST)
echo date('c', $time);
However the format you posted above - 201300 - cannot being understood by strtotime(). There are two problems :
00 is not allowed for the week. Allowed values for week are from 01 to 53
You missed the 'W' in the middle. But it belongs to the ISO 8601 standard.
In my tests I further figured out, that 2012W53 points to the 2012/12/31 not 2013W00 (or W01 as you may think)
Further you may read this comment in the php manual and the Wikipedia article on this.
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 ;)
I'm not sure that this is a bug since after searching I can't find any duplicate experiences- however, this one has me stumped.
While in the midst of a (rather painful) script that is intended to take a bunch of freetext records and convert them to useful date records, my trusty friend strtotime() seems to have let me down.
For testing purposes, I boiled the code down to this:
<?=date('Y', strtotime("1999"));?>
Output shows: 1999
<?=date('Y', strtotime("1981"));?>
Output shows: 1981
<?=date('Y', strtotime("2001"));?>
Output shows: 2012
<?=date('Y', strtotime("2021"));?>
Output shows: 2012
Everything seems fine until the input exceeds "1999"- From that point on, every year before and after the current one returns the current year (2012)
Any input is much appreciated.
As per PHP's date/time format docs:
The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.
(2nd last note on the page).
Try prefixing the years with Jan 1,.
For example:
<?=date('Y', strtotime("Jan 1, 2021"));?> outputs 2021 as expected.
I'm supposing this is because certain years can be incorrectly parsed as month/day pairs, such as "2012" being interpreted as "December 20th of the current year".
If you want proof for yourself, try changing the date format to r:
<?=date('r', strtotime('2001'));?> gives Thu, 23 Feb 2012 20:01:00
The problem is, that it is parsed as time, what you can see if you use date('c') instead of date('Y');.
php > var_dump(date('c', strtotime("2001")));
string(25) "2012-02-23T20:01:00+01:00"
You should pass the value unambiguous for example 2012-01-01.
Another solution is to use a function, that allows to specify the format of the given input, like strptime(), or DateTime::createFromFormat()
php > echo DateTime::createFromFormat('Y', '2001')->format('c');
2001-02-23T22:29:56+01:00
Use this:
echo date('Y', strtotime("1/1/2001"));
echo date('Y', strtotime("1/1/2021"));
Using 4 digits as date can be interpreted as time, you should use a more specific format to make sure the function works as you expect it.
So 2021 is 20:21 (24h format) of 2012
$plaintext = '2004'; //'2004-11','2004-5','2004-5-1','2004/5-1','2004/08/10'
echo date('Y', strtotime(
(strlen($plaintext)==10?$plaintext:
(strlen($plaintext)==7? str_replace('-','/',$plaintext).'/01':
(strlen($plaintext)==4?$plaintext.'/01/01':$plaintext)
)
)));