I'm using the LastFM API to extract a user's recently listened-to tracks (http://www.last.fm/api/show/user.getRecentTracks) and am struggling to shift the timestamp to match my preferred timezone.
I've used date_default_timezone_set at the beginning of the code, but that seems to be ignored when I use strtotime. I'm using strtotime so that I can reformat the styling of the date as Last.FM provides it.
I've figured out how to manually offset to the correct time, via $date - 14400, but I'd like to understand what I'm missing and make the adjustment in the correct way.
Code follows. Greatly appreciate any assistance.
<?php date_default_timezone_set('America/New York'); ?>
<?php $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026");
echo '<ul>';
foreach($xml->recenttracks->track as $track) {
$title = $track->name;
$date = $track->date;
$date = strtotime($date);
$date = date("F jS, g:i a e", $date);
$string = '<li>'.$title.' - '.$date.'</li>';
echo $string;}
echo '</ul>';
?>
The problem is that the default timezone you set is used for both the incoming and outgoing translation.
To solve, use PHP timezone functions, and flip it about when reading / writing the time:
$oDateTime = new DateTime($track->date, new DateTimeZone(UTC'));
echo $oDateTime->format('F jS, g:i a e') . "\n"; // For debug: Will give the same back at you.
$oDateTime->setTimezone(new DateTimeZone('America/New York'));
$date = $oDateTime->format('Y-m-d H:i:sP') . "\n"; // Will give the converted date
(You can also do it with date_timesone_set, but this looks neater).
Related
I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');
I am building a PHP service that will allow people to click on an Add to calendar button on their mobile devices.
How do I convert 2009-09-12 15:00 into the example timestamp I have been given 20091109T101015Z using PHP?
I have played around with strtotime gmdate but I think I am just missing the correct format parameter.
Thanks
Try this:
$date = '2009-09-12 15:00';
$formatted_date = gmdate('Ydm\THis\Z', strtotime($date));
Did you try to use formating?
$date = new DateTime("2009-09-12 15:00");
$formated = $date->format("Ydm\THis\Z");
For reading: http://php.net/manual/ru/function.date.php
Here you go:
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("YmdTG:iz", $the_date) . "<br />");
I'm upload the excel data into mysql. in there date save like 16.5.59(dd.mm.yy) formate. I try to change this using this code
$date = '16.5.59';
echo date('d-m-y',strtotime($date));
it always show current time like 04-02-15.
Pls change this to dd-mm-yyyy formate.
Thanks in Advance.
Timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
If it not cover the between given range then it will take current date.
$date = '16.5.59';
$date_array = explode(".",$date);
$var_day = $date_array[0];
$var_month = $date_array[1];
> Blockquote
$var_year = $date_array[2];
echo $new_date_format = "$var_day-$var_month-$var_year";
Try to make the date yourself using explode and strtotime:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_time = strtotime(implode('-', array_reverse(explode('.', $date))))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
Output: 16-5-2059
You ARE required to specify that the year is 1959 instead of 2059 (or are you sure that it's really 2059?)
You can make change from 2059 to 1959 like this:
date_default_timezone_set('America/Los_Angeles');
$date = '16.5.59';
$date_arr = array_reverse(explode('.', $date))
# This line does the job. Make sure all years are between 1900 - 1999.
$date_arr[0] = '19' . $date_arr[0];
$date_time = strtotime(implode('-', $date_arr))
$date_str = date('d-m-Y', $date_time);
echo $date_str;
I hope this answer can help you.
This is by no means the best solution but using php's date_parse_from_format is possible. You could supply a format and use the following
$date = "16.5.59";
$dateObj = date_parse_from_format("j.n.yy", $date)
with the $dateObj you can work what you need such as :
echo $dateObj['year'];
IDE Running Example
Worth noting you require PHP >=v5.3
You can do this if it is for simpler task. It will consume time.
$date = '16.5.59';
$dtSplit=explode(".",$date);
echo "<br>".$dtSplit[0].".".$dtSplit[1].".".$dtSplit[2];
echo "<br>".$dtSplit[0].".".$dtSplit[2].".".$dtSplit[1];
echo "<br>".$dtSplit[1].".".$dtSplit[0].".".$dtSplit[2];
echo "<br>".$dtSplit[1].".".$dtSplit[2].".".$dtSplit[0];
echo "<br>".$dtSplit[2].".".$dtSplit[0].".".$dtSplit[1];
echo "<br>".$dtSplit[2].".".$dtSplit[1].".".$dtSplit[0];
Hi guys i am really new to php and i am trying to convert the timestamp from an xml array but with no sucess , i read everything i found but still can find the way can you please help ?
I am using this code to decode an xml api output
$mysongs = simplexml_load_file('http://example.com/test/xml/');
$timestamp = $mysongs->item->timestamp;
$playtime = date("Y-d-m G-i-s",$timestamp);
If i echo $timestamp it works fine, $playtime doesn't...
Tried with :
echo gmdate("Y-m-d", $timestamp);
&
echo date('Y-m-d H:i:s', $wra);
&
echo '<timeplayed>' . date('Y-m-d G:i:s', $mysongs->item->timestamp) . '</timeplayed>';
Still no luck.. Time is not showing.. If i use this example
echo '<timeplayed>' . date('Y-m-d G:i:s', (1365532902)) . '</timeplayed>';
it works fine.. What am i doing wrong here ?
Update
Finally i found it.. it needed to cast the $timestamp as integer for the date to decode properly as euxneks sugested..
So right code should be
$timestamp = intVal ($mysongs->item->timestamp);
and then
$playtime = date("Y-d-m H-i-s",($timestamp));
& finally echo ($playtime); and it works fine...
Thanks everyone for your replys problem solved
It's likely that you need to cast your result as an integer (it's been a while since I've used simplexml but I'm pretty sure it doesn't automatically type the values received):
http://php.net/intval
Also, strtotime might work too :)
http://php.net/strtotime
You are definitely missing strtotime function, here is an example:
$str = '04/09/2013';
$date = date('Y-m-d H:i:s',strtotime($str));
echo $date;
This will output:
2013-04-09 00:00:00
Updated
Since strtotime is alread used, what about using:
$date = date('Y-m-d H:i:s', ($timestamp));
i fetched from created field date and time like this format 2011-3-10 17:26:50 and i want to convert to this format March 13, 2010 how can i do this with php or cakephp 1.3
<?php
$i = 0;
foreach($articles as $$article ):
?>
<tr>
<td><?php echo $article['Article']['title']; ?></td>
<td><?php echo $article['Article']['created'];;?></td>
</tr>
<?php endforeach; ?>
I presume you mean "fetched" as in retrieving from MySQL. The simplest/quickest (but also the most like to blow up and kick your dog) is to simply do
$timestamp = strtotime($date_from_database);
$formatted = date('F j, Y', $timestamp);
PHP has a function strtotime($string), that will take dates/time strings in a variety of formats (including MySQL's datetime format) and convert it to a Unix timestamp integer (number of seconds since 1970-01-01 00:00:00 UTC). You can then use date('F j, Y', $time) to convert that integer into whatever string representation you want, using the tokens found here
Two additional considerations are localization and timezone awareness. I won't go into the first since it doesn't seem like you need it, but where timezone matters, it can be easier to use PHP's DateTime classes, which you can read about [here]. Here's an example:
<?php
// for example, if you're storing times in UTC in your DB
$dbTimezone = new DateTimeZone('UTC');
// we'll use this for displaying times in the user's timezone.
// for example, my timezone:
$displayTimezone = new DateTimeZone('America/Toronto');
foreach ($articles as $article):
// Create a timezone-aware DateTime object from your article's creation date
$dt = new DateTime($article['Article']['create'], $dbTimezone);
$dt->setTimezone($displayTimezone);
echo $dt->format('F j, Y');
endforeach;
something like this ?
I'm guessing your datetime format. you may need to adjust it.
echo date('F d, Y',date_create_from_format('Y-n-d h:i:s','2011-3-10 17:26:50'));
date_create_from_format
This will solve it:
$date = date_create_from_format('Y-m-j H:i:s', $article['Article']['created']);
echo date_format($date, 'F d, Y');
You could easily use the Cakephp Time Helper.
//Add to appcontroller or articles_controller
var $helpers = array('Time');
//Add this to view file
echo $this->Time->niceShort($article['Article']['created']);
Besides niceShort there are more options that might fit your needs better. Check CakePHP documentation.
Thanks,