timestamp to php from timestamp array - php

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

Related

Change time format from 04:00 to 00:04:00

I currently get duration in this format: 04:00 but I need in this format 00:04:00.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00
The problem is the strtotime($duration); It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date function.
If you want to just simply add 00: before $duration, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime function, and would likely get better results.

convert dd-mm-yyyy to yyyy-mm-dd in php

I tried the following code to change the dateformat from dmy to ymd, but when using i got wrong dates.
My code
$sdate11=date("Y-m-d", strtotime($_POST["txtstartdates"]) );
$sdate111=date("Y-m-d", strtotime($_POST["txtenddates"]) );
dates inserted were
30-05-2013 and 31-05-2013
the date it returned was
2035-11-03 and 2036-11-02
could you please help me to find what was the problem here and solve it
Thank you.
Try with split like
$a = split('-',$_POST["txtstartdates"]);
or you can use explode even like
$a = explode('-',$_POST["txtstartdates"]);
$my_new_date = $a[2].'-'.$a[1].'-'.$a[0];
Here strtotime will not work for the format dd-mm-yyyy
You might use DateTime for that:
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtstartdates']);
echo $date->format('Y-m-d');
$date = DateTime::createFromFormat('d-m-Y', $_POST['txtenddates']);
echo $date->format('Y-m-d');
Can't you use the DateTime object to convert the date into the format you want?
$DateTime = new DateTime($_POST['FIELD']);
echo $DateTime->format('Y-m-d');
Your code seems correct , I dont know why its not working for you Can you try below
I have made the '' from "" only
$sdate11=date('Y-m-d', strtotime($_POST['txtstartdates']));
$sdate111=date('Y-m-d', strtotime($_POST['txtenddates']));
You can do it using single line as:
$show_date = DateTime::createFromFormat('d-m-Y', $dateInput)->format('Y-m-d');

Properly Update A Timestamp's Timezone

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

Converting String to DateTime in PHP

I need to insert the datetime value entered from the HTML form using PHP into the MySQL database. However I receive the Incorrect datetime value error each time I try to execute the code below,
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
echo date('d/M/Y H:i:s', $date);
// echo DATE_FORMAT($rosterstartdate,"%Y%m%d %H%i%s");
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts','$date','$rosterenddate')";
I have tried using each of the different techniques above to do the conversion but it does not work. Any ideas on how this could be inserted
try this:
$date = date('Y-m-d H:i:s', $date);
Instead of echoing it out, use that code to format the date.
However, it looks like what you really want is this:
$rosterstartdate = date('Y-m-d H:i:s', strtotime($rosterstartdate));
$rosterenddate = date('Y-m-d H:i:s', strtotime($rosterenddate));
This way you can just reference those two variables in your query string.
You don't need to format it if you have a unixtime use FROM_UNIXTIME,
change your query as
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts',FROM_UNIXTIME($date),'$rosterenddate')";

PHP Print Date Variable Format

So this should be a real easy question but I can't seem to find a simple answer anywhere.
I'm patching up some PHP code (I'm not a PHP'er) and I have this variable $orderDate. How do I print this variable so that its just M/d/yy h:mm tt?
Update:
So I looked around and saw what $orderDate is. Here's the code:
global $orderDate;
$orderDate = strftime('%c');
print("Order Date: ".date("M/d/Y h:M", $orderdate)."<br />");
so I get this for output:
Dec/31/1969 06:Dec
and should be getting today's date....
echo date("m/d/Y h:m", $orderDate);
echo date("m/d/Y h:m", strtotime($orderDate)); // or this
Depends on what $orderDate contains.
Look into date() since it has there plenty of examples and is pretty simple to use.
UPDATE:
$orderDate = date("M/d/Y h:M");
print("Order Date: ".orderDate ."<br />");
Also check out to see if this works for you.
date function will do that for you.
If $orderDate is an integer time stamp, you probably want strftime. Specifically, I think the call you want would be:
strftime("%D %l:%M %p", $orderDate)
However, I recommend reviewing the web page to make sure I've interpreted what you want correctly.
See the PHP date() function.
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
string date ( string $format [, int $timestamp = time() ] )

Categories