Not sure whats up with the code
$date = strtotime("%b %d, %Y", $datedata);
$time = strtotime("%I:%M:%S %p", $datedata);
The time i gets from the DB is 1298747601 and is the $datedata
I have date_default_timezone_set('America/Los_Angeles'); at the top of the script.
Use strftime() or date(), not strtotime(). Strtotime is meant to format textual dates, not timestamps.
It's better to use the DATETIME datatype in your database instead of an int containing a timestamp. Using a DATETIME means easier calculating with dates and you can format the date directly in your query.
strtotime() is for converting a string to a time like, $timestamp = strtotime("1:33 PM EST Next Friday");.
You want date(), which takes a format string and a timestamp to create a formatted date time string, and uses the current time (from time()) if no timestamp is passed, like date("h:i:s A T, M jS, Y", $timestamp) which would output something like "1:33:00 PM EST, March 4th, 2011". Also, note that if you want to do words in the format, like "23rd of January", and the letters in the words are also date formatting characters, in this case the 'o' in 'of' is the ISO-8601 year number formatting option, you must escape it with a \ slash like "jS \of F". As the f is not a formatting option, it does not need to be escaped, but if it did, then it would be "\o\f"
There's also a DateTime object that's built into more recent PHP versions that you can look into.
I disagree with Ray that it is better to store a DateTime datatype in SQL rather than an integer timestamp. They're about the same. You can still search for date ranges in the DB simply by finding numbers greater than X timestamp and less than Y timestamp, and, as a plus, everything that you get from the DB is already in a timestamp format that can be used easily by PHP without having to convert it to a proper timestamp.
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 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));
In my PHP script I've got a function handling birthdays like so:
$dateTime = \DateTime::createFromFormat('U', $time);
The problem is that this returns false with negative $time numbers (i.e. dates before 1-1-1970). In the PHP docs there's a comment saying that indeed
Note that the U option does not support negative timestamps (before
1970). You have to use date for that.
I'm unsure of how to use Date to get the same result as DateTime::createFromFormat() gives though. Does anybody have a tip on how to do this?
If you just need to format a UNIX timestamp as a readable date, date is simple to use:
// make sure to date_default_timezome_set() the timezone you want to format it in
echo date('Y-m-d H:i:s', -12345);
If you want to create a DateTime instance from a negative UNIX timestamp, you can use this form of the regular constructor:
$datetime = new DateTime('#-12345');
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.
I have date and time stored in my database as MySQL DATETIME datatype. While inserting into the database, I am using the following PHP variable
$serverTime = strftime("%Y-%m-%d %H:%M:%S",$_SERVER['REQUEST_TIME']);
A sample DATETIME stored in database is: 2011-11-26 01:00:27
Now my website needs the date to be displayed in the following format: November 26, 2011 (time is not required)
How can I do it? I am trying the below in PHP but wrong output I am getting.
echo strftime("%B %d, %Y",$serverTime)
The PHP strftime() function wants input to be a 32-bit integer timestamp, which is the number of seconds since 1970-01-01 00:00:00.
The default output format for MySQL datetime is YYYY-MM-DD HH:MM:SS. When you use this string in PHP, the leading digits are converted into the integer 2011, which is interpreted to be within the first hour of 1970-01-01.
So you must fetch the datetime from MySQL in another format. Choices:
UNIX_TIMESTAMP(datetime_column) to fetch the datetime as an integer that you can give to PHP's strftime() function.
DATE_FORMAT('datetime_column', '%M %d, %Y') to format the datetime as a string, and then you don't have to use strftime() to format it.
While I would recommend a completely SQL approach, you can use PHP's DateTime class to process your string in PHP (assuming you're running PHP > 5.3):
// Pick a valid TimeZone
$date_obj = DateTime::createFromFormat( 'Y-m-d H:i:s', '2011-11-26 01:00:27', new DateTimeZone( 'America/New_York'));
echo $date_obj->format('F d, Y');
Demo