ymakux
ymakux

Reputation: 3495

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?

Upvotes: 0

Views: 719

Answers (2)

rjdown
rjdown

Reputation: 9227

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

Upvotes: 2

ourmandave
ourmandave

Reputation: 1585

date() expects the $date to be a timestamp which is the number of seconds since the Unix epoch. Try converting $date with strtotime.

Upvotes: 1

Related Questions