Issue with date_create_from_format() - php

I am using a plugin to create wordpress posts from a twitter feed, and I am trying to edit it so the published time is the same as the tweeted time, rather than the time the cron was run.
Unfortunately, twitter's API returns an already formatted date string, instead of a timestamp, so I am having to parse it and then save it in a wordpress friendly format.
// Wed Jun 06 20:07:10 +0000 2012 (Twitter formatted date example)
// 2014-03-10 18:30:26 (Wordpress formatted date example)
$tweet_date = $tweet->created_at;
$tweet_date = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = date("Y-m-d h:i:s", $tweet_date);
Unfortunately, all I am getting from this the Unix Epoch (Jan 1st, 1970).
I know I must be missing a step, but I can't figure out where.

You had two issues:
1) You were using h for hours when you meant H for 24 hour periods
2) You need to use date_format() when using date_create_from_format() as that function returns a DateTime object which is not compatible with date()
$tweet_date = date_create_from_format("D M d H:i:s O Y", 'Wed Jun 06 20:07:10 +0000 2012');
echo date_format($tweet_date, 'Y-m-d H:i:s');
See it in action

The problem is because you're mixing and matching between PHP's old and new-style date handling.
date_create_from_format() is part of the newer API, and outputs a DateTime object, not the timestamp integer that the older date() function is expecting.
Ideally you should stick entirely with either the new or the old date functions. You can switch between them, but there usually isn't a need to.
For example, in your case, the DateTime object generated by date_create_from_format() has a perfectly usable format() method attached to it, which does exactly the same as the date() function, but on a DateTime object.
$tweet_date_object = date_create_from_format("D M d h:i:s O Y", $tweet_date);
$tweet_date = $tweet_date_object->format("Y-m-d h:i:s");

Related

php date does not match database?

This is the coding and it is echoing with the right format but the fact that the date is wrong when it prints out.
Output: 31 Dec 1969 19:33
Database Timestamp 2016-05-20 21:53:17
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i',$row['timestamp']);
?>
and i have tried doing the date in different ways and still the same result
In the code sample you posted, $row['timestamp'] has not been set, so the date is constructed with timestamp 0, also known as epoch, or the date that is being echoed.
If you change it as follows, it should work fine:
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i', $timestamp); ?>
Side note:
Time zone ECT is not a valid time zone code in PHP. If I assume correctly that you mean european central time, you would have to specify CET instead.
ECT doesn't exist as a valid TimeZone, did you mean CET perhaps?
The correct way to do this is using the DateTime class, i.e.:
$date = new DateTime();
$date->setTimestamp(1456778973);
$tz = new DateTimeZone("America/Denver");
$date->setTimezone($tz);
echo $date->format('d M Y H:i');
PHPFiddle Demo
Note:
Dates should always be stored in DB as UTC (timestamp aka unix time), then you can add or subtract the timezone offset using the DateTime class.
Would you know what the offset would be for mountain standard time?
Mountain Time: America/Denver
Mountain Time (no DST): America/Phoenix
List of Supported Timezones

Displaying actual time using iso 8601

I m referring to this: Converting ISO 8601 format to d M Y in PHP
I tried this same as this: echo date("d M Y H:i:s", strtotime($time));
But time is not shown as saved in database. Its showing few hours difference.
Database has: 2016-03-20T23:30:51+00:00
With above php echo i get: 21 Mar 2016 00:30:51
Where as it must be 20 Mar 2016 23:30:51
Above example shows additional of 1 hour y?
I tried using this format to display hour & minute but time shown is wrong. Time display has few hours difference. Why is this so?
Your date format 2016-03-20T23:30:51+00:00 reveals a GMT DateTime (side note: the same TimeZone used by php/unix timestamps).
So when you write:
echo date( "d M Y H:i:s", strtotime( $time ) );
You obtain yet the correct date converted in your system TimeZone.
You can use DateTime class to perform more operations with dates:
$date = new DateTime( $time );
echo $date->format("d M Y H:i:s");
will print the date in original (GMT) format.
$date->setTimezone( new DateTimeZone('Europe/Berlin') );
echo $date->format("d M Y H:i:s");
will print the date in 'Europe/Berlin' Timezone.
Side note: saving dates in ISO 8601 UTC/GMT is actually the best choice.
Read more about DateTime
Read more about DateTimeZone
Your system is using its local timezone instead of UTC, whereas the database is in UTC. If you wish to display in UTC, you can use http://php.net/manual/en/function.date-default-timezone-set.php to set the timezone to UTC before calling strtotime and date.

How to change format of date from that stored in db [php]?

I am storing date time in database as $date=date("Y-m-d H:i:s"); Now on most places I use it in the same format as it is stored 2016-03-01 19:04:18, but on one place I would rather prefer output to be March 01 at 19:04 how can I change format of output from database from 2016-03-01 19:04:18 to March 01 at 19:04.
Working with dates in PHP
First of all we will need to retrieve the date from the database. You can do this however you please.
What we are going to do next is to pass your date to a PHP DateTime object. This will give us easy options to modify the format to our liking.
// $dbDate is the date you got from your database
$date = new DateTime($dbDate);
// If we want to make sure PHP uses the correct date format,
// We can also use createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dbDate);
Now that we have this object we are going to output it in the format you want it to be.
echo $date->format('M d').' at '.$date->format('H:i');
// M - Textual representation of the month ex. January, February, March
// d - Day of month in numeric value ex 1, 14, 27
// H - 24-hours representation of the hour ex 17, 23, 08
// i - Minutes ex 23, 45, 58
For more parameters and information:
PHP.net - DateTime()
Format it like this
date('d-M-Y H:i');
instead of
date("Y-m-d H:i:s");
Play around with these d-M-Y H:i to get the desired result
parse the date variable to it to tell it which variable to format
date("Y-m-d H:i:s", $date);
also see documentation here on date function in PHP
http://php.net/manual/en/function.date.php

How do I format this time/date in PHP?

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

How can I get today's timestamp in PHP

I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.

Categories