PHP Date format when converting from user input (code igniter) - php

I am taking a value entered by a user in a jQuery UI datepicker and converting it to a to a php date for insertiion in a database like so:
$date_from = post('date_valid_from');
$date_from = date('Y-m-d', strtotime($date_from));
This works fine if the input is formatted in US format (mm/dd/yyyy), however as my system will be used by people in the UK it needs to be input in the UK format (dd/mm/yyyy). When converting the UK dates to a php date, the month and day are switched around (Feb 1 2014 - 01/02/2014 - becomes January 2 2014 and Feb 28 2014 - 28/02/2014 - becomes January 1 1970).
Is there anyway I can overwrite the default date format to work with UK format?
(This project is using Code Igniter if there is a CI config setting I can set)

Use DateTime() instead. It's more flexible than date() and strtotime():
$date_from = DateTime::createFromFormat('d/m/Y', $date_from);
$date_from = $date_from->format('Y-m-d');
If you want to support both formats (mm/dd/yyyy and dd/mm/yyyy) just swap out the first parameter of DateTime::createFromFormat(). Using a variable would make that easy to do.

This is a good example case for localization in PHP.
To format a local time/date according to locale settings you can use strftime
http://php.net/strftime
Then can parse a string using a specific format with strptime
http://php.net/manual/en/function.strptime.php
For more information about localization you can check this link as well.
http://php.net/manual/en/function.setlocale.php

Related

How to use PHP to get DateTime from XML node? [duplicate]

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));

PHP Date Formatting not working wen using date_format()

I have a date field I'm pulling from WordPress, where the default format is:
16/12/2016
Been a while since I played with PHP, but I remember the way to format dates was: echo date_format($date,"[format syntax]");
But when I do
$date = myWPField;
echo date_format($date,"F d, y");
If doesn't display the date.
What am I missing. Has this changed?
Since your date is not an acceptable format in date_create like mm/dd/yy(yy) or mm-dd-yy(yy)
Use php DateTime::createFromFormat function instead this function can detect what format you
(PHP 5 >= 5.3.0, PHP 7)
DateTime::createFromFormat -- date_create_from_format — Parses a time string according to a specified format
$date = '16/12/2016';
$show_date = DateTime::createFromFormat('d/m/Y', $date)->format('F d, y');
Demo
when create data format form string use slash format, it's in the mm/dd/yy(yy) format. So in you code your first 16 is not allowed by default, you have to specify in what format to create from the string.
Note that when you create a new date object using a format with slashes and dashes (eg 02-02-2012 or 02/02/2012) it must be in the mm/dd/yy(yy) or mm-dd-yy(yy) format (rather than british format dd/mm/yy)! Months always before years (the american style) otherwise you'll get an incorrect date and may get an error like the one above (where PHP is crashing on trying to decode a 13th month).

Converting date from xml to readable format

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));

convert mysql timestamp into php user friendly version

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.

How to make date time like this?

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));

Categories