How do I convert this timestamp to PHP? Bing Maps API - php

I've been playing around with the Bing API using json and PHP. The array spits out the following for dates:
[end] => /Date(1354867200000)/
[lastModified] => /Date(1349441488000)/
I thought this was a unix timestamp, but it I don't think it is. What I did was a preg_replace like this
$last_updated = $resource->lastModified;
$last_updated_timestamp = preg_replace('/[^0-9.]*/','',$last_updated);
Then tried to convert it to a date
$last_updated_date = date('l F d Y g:i:s A',$last_updated_timestamp);
The results that it's showing me for date range back from the year 1967 to 2000. Is this a different kind of timestamp that I don't know of? If so, how do I correct this? Any help would be appreciated!

The number part is milliseconds-since-the-Epoch (January 1, 1970 at midnight — the milliseconds version of a unix timestamp). This is a fairly conventional way to represent dates in JSON (since JSON doesn't have a date type).
So getdate(theNumber / 1000) will give you the date (since getdate expects seconds, not milliseconds, since The Epoch).

If what you want to do is convert a unix timestamp to date format, you can do it by doing the following:
date("F j, Y g:i a", strtotime($unix_timestamp));
Where $unix_timestamp is your unix timestamp in this case.
You can always print it out for testing purposes by adding echo before it.
So in this case it could be:
$last_updated_date = date("F j, Y g:i a", strtotime($last_updated_timestamp));

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

Calculate Future PHP Date in Specific Format (dd month yyyy hh:mm:ss)

Extreme PHP newbie here - I am trying to create a PHP variable that will be "CURRENT DATE + 7 Days"
Something like :
date('D-m-y H:i:s', strtotime(DateTime("+7 day"))
However, I need it to output in a format like this: "30 November 2015 09:00:00"
Any ideas?
Thanks in Advance!
You can check the manual for valid date formats and change your format string.
You're basically looking for date('j F Y H:i:s', strtotime("+7 day"))
Personally, I recommend working with DateTime if you're storing this in a variable and working with it, because it becomes more convenient to extract the formatted date from the object at your conveience any time without having to go back through date and strtotime each time. Also there are numerous other benefits like not losing timezone information during conversion or having to change global timezones that effect the conversion, etc...
Example
$date = new DateTimeImmutable; // today's date
echo $date->modify('+7 days')->format('j F Y H:i:s'); // 7 days from today
echo $date->modify('-7 days')->format('j F Y H:i:s'); // 7 days ago

Convert google analytics date to human readable value

I am getting Google Analytics data via API using google-api-php-client
Everything fine except one thing, I can't convert day timestamp into readable value. The day timestamp looks like 20150724, date('M j', $date); always shows Aug 22 even for different timestamps.
How to fix it?
If you don't tell PHP what the number is, it will assume it's a UNIX timestamp. 20150724 is 22nd August 1970...
So just tell it how it's formatted:
<?php
$google_date = '20150724';
$date = DateTime::createFromFormat('Ymd', $google_date);
echo $date->format('M j');
date() expects the $date to be a timestamp which is the number of seconds since the Unix epoch. Try converting $date with strtotime.

Issue with date_create_from_format()

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

convert mysql timestamp into actual date and time?

I have dates stored in a mysql table, they are set to store as CURRENT TIMESTAMP in each row and are stored as follows:
2010-05-29 01:17:35
but what i am trying to do is somehow use PHP to be able to seperate everything and create a date more like:
May 29 2010 1:17 AM
can anyone at least direct me in the right path that i should take. any help is greatly appreciated!
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
http://php.net/manual/en/function.date.php
You have two solutions :
Use strtotime() to parse the date to a timestamp, and date() to re-format it to a string
Or use the DateTime class
In PHP code, this would mean using :
echo date('M j Y g:i A', strtotime('2010-05-29 01:17:35'));
Or :
$dt = new DateTime('2010-05-29 01:17:35');
echo $dt->format('M j Y g:i A');
strtotime + date is the solution you'll see used the most ; but it is not the best solution : with those, you'll work on UNIX Timestamps, which means a limited range of dates (from 1970 to 2038, if using 32 bits integers).
ON the other hand, using the DateTime class, there will be no limit to the range of dates you can work with.
If you have a DATETIME field in your table and you only want the date field, then:
SELECT DATE_FORMAT(timestamp,'%M %D, %Y') FROM Mytable;
or:
SELECT DATE_FORMAT(timestamp,'%Y-%m-%d') FROM Mytable;
where timestamp is your column name.

Categories