i have 2011-08-03 21:56:41 coming from a MySQL timestamp and I would like to convert it to Wednesday August 3rd, 2011 using PHP (Not MySQL). How can this be done?
Use strtotime() to convert your date/time string to a Unix timestamp so you can use date() to format the value of that timestamp any way you want.
$stamp = '2011-08-03 21:56:41';
echo date('l F jS, Y', strtotime($stamp));
// output: Wednesday August 3rd, 2011
The reference at the date() manual page is extremely useful. I still reference it all the time for the list of special format characters.
The strtotime() function sort of seems like magic at first. For future reference, here's the supported date/time documentation on what input formats strtotime() can accept.
Related
I have a xml file, containing several dates, in this format: 2016-07-23T07:00:00.000Z. I'm using a php function to convert this in to a format for publishing on a website. This should actually result in something like Saturday, 24th of July (24th, not 23rd, because of the time offset. My function somehow ignores the T07:00:00.000Z part and thus returns Friday, 23rd of July. Can anybody help me out with the proper way to convert this date?
Thanks, Peter
The string in question
2016-07-23T07:00:00.000Z
is a W3C datetime format (W3C DTF) (Complete date plus hours, minutes, seconds and a decimal fraction of a second) which can be properly parsed incl. the fractions of a second with the date_create_from_format](http://php.net/date_create_from_format) function:
$originalDate = "2016-07-23T07:00:00.000Z";
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
It does create a new DateTime which then can be formatted with the for PHP standard codes, e.g.
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->format('Y-m-d H:i:s'); # 2016-07-23 07:00:00
As that W3C format carries the timezone already and it is UTC, and you wrote you want a different one, you need to specify it:
date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate)
->setTimezone(new DateTimeZone('Asia/Tokyo'))
->format('Y-m-d H:i:s');
The reason why this is not visible (and controlable with the code given) in the previous answer is because date formats according to the default set timezone in PHP where as each DateTime has it's individual timezone.
An equivalent with correct parsing (incl. decimal fraction of a second) with the other answers then is:
$dateTime = date_create_from_format('Y-m-d\TH:i:s.uO', $originalDate);
date('Y-m-d H:i:s', $dateTime->getTimestamp());
Hope this explains it a bit better in case you need the complete date value and / or more control on the timezone.
For the format, see as well: In what format is this date string?
$oldDateTime= "2016-07-23T07:00:00.000Z"; // Your datetime as string, add as variable or whatever.
$newDateTime= date("Y-m-d H:i:s", strtotime($originalDate));
I have a set of dates that are formatted like this...
197402
192201
184707
The first four digits represents the year and the remaining two the month. I am trying to output these in this format
February 1974
January 1922
July 1847
I have tried passing it to the date function like this...
echo date ('F Y', 197402)
But this is giving me January 1970 everytime so I assume I have misunderstood how the date function works, can anyone help?
You're getting "January 1970" as an output, because you tried to create a date from the timestamp 197402, which is seconds from January 1st, 1970. If you output the full string from that (with seconds and whatnot), you'll see it's a valid timestamp, producing an actual date, but they all end up in the start of January 1970, see this online demo.
That format, YYYYMM, isn't a recognizable format for most functions. You need to split it up, if you know the format will be in that way - and use that data instead. You can use substr() to split the string, and then convert the numerical month to the string associated with that month, with the help of date() and mktime() (since you just specify the year and month).
The following snippet
$arr = [197402, 192201, 184707];
foreach ($arr as $v) {
$year = substr($v, 0, 4);
$month = substr($v, 4, 2);
echo date("F Y", mktime(0, 0, 0, $month, 0, $year))."<br />"; // mktime() produces a valid timestamp based on just month and year
// Alternatively, drop mktime() and use strtotime() and create from a standard format,
// while specifying a date in the month (which won't matter to the output)
// echo date("F Y", strtotime("$month/01/$year"))."<br />";
}
will output
February 1974
January 1922
July 1847
Alternatively, you can use the DateTime class (which is a lot simpler to work with), and create from a given format with date_create_from_format()
foreach ($arr as $v) {
echo date_create_from_format('Yh', $v)->format('F Y')."<br />";
}
This will generate the same output as above.
References
http://php.net/substr
http://php.net/mktime
http://php.net/date
http://php.net/datetime.createfromformat
I'd use the DateTime class, you can create from a specific format, and then output to another.
As pointed out in the comments below, you also need to set the day to the first of the month, otherwise you'll get undesired results if the current day is greater than the number of days in the given month.
echo DateTime::createFromFormat('Ymd', 19470201)->format('F Y');
As you are passing an int to date, it is considering it as a Unix Timestamp.
To create a date object from a predefined format, use DateTime::createFromFormat.
echo DateTime::createFromFormat('Ym',198403)->format('F Y');
results in
March 1984
You'd need to add a day to it, e.g. just add "01" and then use strtotime to convert that into a unix timestamp, as the date() function expects a timestamp as the parameter.
e.g.
echo date('F Y', strtotime("19220101"));
You have text representation of dates in a non-standard format. First you have to parse them and convert them to timestamps (the number of seconds since Jan 1, 1970 00:00:00 UTC). The PHP function date() can work only with timestamps.
The best approach (as of 2017) is to use the DateTime PHP class for date & time processing:
foreach (array('197402', '192201', '184707') as $text) {
$date = DateTime::createFromFormat('Ym', $text);
echo($date->format('F Y')."\n");
}
The method DateTime::createFromFormat() parses a string using the given format and creates a new DateTime object if the parsing succeeds. It is the OOP equivalent of strtotime() but smarter (because it can get hints about what date components to search in the input string.)
The method DateTime::format() produces the text representation of a date using the provided format. It is the OOP equivalent of date().
The OOP approach (the DateTime* classes) is recommended (and better than the procedural approach) because has built-in support for timezones (the procedural date-time functions lack it.)
I am receiving JSON data with a date string with this format:
'Mon Jun 30, 2014'
What would be the way to convert this to a datetime? Checking the PHP functions for this I got unsecure if better to use date_parse_from_format or date_create_from_format.
What are the differences and which would suit better for this task?
DateTime::createFromFormat would work well here. It allows you to format the string easily and also takes timezones into consideration when appropriate and can be easily used in comparisons without having to convert to a timestamp first.
$date = DateTime::createFromFormat('D M d, Y', 'Mon Jun 30, 2014');
echo $date->format('Y-m-d');
You can convert to a unix timestamp using strtotime(). I don't know what you mean by a "datetime", but if you mean something like for MySQL then you format the timestamp with date() (you can include time but it isn't present in the original string):
echo date('Y-m-d', strtotime($string));
The second of the two likely fits you better ---
The first one only breaks down the date into an array, so you can work with the parts, individually.
But the second returns the DateTime object you are looking for.
When I make projects on my website, I've saved the time and date that I made them on in MySQL as '2012-02-27 12:32:36' format.
How can I get this to display as:
"February 27, 2012 12:32 p.m."?
I've looked at other date formatting on PHP and read on the PHP website, but all of the functions they provide don't seem to be for grabbing a date in the format that I've saved it in.
First, convert the MySQL DateTime string you have into a DateTime object by using DateTime::createFromFormat()
$mysqlDateTime = '2012-02-27 12:32:36';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $mysqlDateTime);
Then, simply call DateTime->format() with the desired format string:
$formattedDate = $date->format('F j, Y g:i a');
You can also use the MySQL UNIX_TIMESTAMP() date function (to extract a unix timestamp compatible with date()) or use strtotime(), however note that these methods do not support dates after January 19, 2038 on 32-bit systems due to overflow (the two methods do not support PHP's automatic number variables promotion).
Hi I am saving data from rss feed url. From that me got date time like this.
Sun, 2 January 2011 03:04:02 GMT+5:30
How to change this date to this format 2nd January 2011, 03:04 PM using php?
any body knows the solution please help me.
You can se the strtotime function to convert the existing string and the 'r' specifier to the date function as follows (looks like you want it in RFC 2822 format, if not tweak accordingly):
date('r', strtotime("Sun, 2 January 2011 03:04:02 GMT+5:30"));
Incidentally, make sure you're setting your local timezone correctly via date_default_timezone_set, etc.
The following functions are useful for taking a string and getting a timestamp back:
strtotime()
DateTime::createFromFormat()
After you have it as a timestamp, you can reformat it using date(). I'm not 100% sure if strtotime() would accept that format, but it should accept it because the format it isn't ambiguous.
echo date("js F Y, h A", strtotime($oldDate));