DateTime data from XML convert to string - php

I'm trying to convert a date format using the DateTime function. The date data is taken from an external XML file, how can I convert the data intro a string in the DateTime function?
Here's the code:
echo "Date: " . $date = new DateTime((string)$info->channel[0]->item[0]->pubDate); echo $date->format("d-m-Y H:m") . "<br />";
This is the error:
Catchable fatal error: Object of class DateTime could not be converted to string in
Something I'm forgetting?

You are using string concantation with a datetime object. You need to convert the date to a string before trying to echo it:
$date = new DateTime((string)$info->channel[0]->item[0]->pubDate);
echo "Date: " . $date->format("d-m-Y H:m") . "<br />";

Related

How to convert DateTime in UTC format?

I have time in this format and I want to convert this into UTC timezone.
: "2015-03-17T07:46:52+0100"
: "yyyy-MM-dd'T'HH:mm:ssZ"
I am using Codeigniter framework.
Pretty straightforward using DateTime objects
$string = "2015-03-17T07:46:52+0100";
$dt = new DateTime($string);
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s');
Try the getTimezone and setTimezone, see the example
(But this does use a Class)
UPDATE:
Without any classes you could try something like this:
$the_date = strtotime("2015-03-17T07:46:52+0100");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");

Convert date string with timezone to timestamp

I receive dates in the following format 2015-01-09T20:46:00+0100 and need to convert it in timestamp.
Unfortunately, strtotime function is ignoring the timezone component :
print strtotime('2015-01-09T20:46:00+0100') . "\n";
print strtotime('2015-01-09T20:46:00');
//result is the same with or without timezone:
//1420832760
//1420832760
What is the right way to solve this issue ?
DateTime handles this correctly:
$date = new DateTime('2015-01-09T20:46:00+0100');
echo $date->getTimestamp();
echo "\n";
$date = new DateTime('2015-01-09T20:46:00');
echo $date->getTimestamp();
1420832760
1420836360
Demo
I fugured out !
uses the default time zone unless a time zone is specified in that
parameter
http://php.net/manual/en/function.strtotime.php
That's why the result is the same setting or not the timezone :
date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
print "\n\n";
date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
output:
1420829160
1420832760
1420832760
1420832760
1420836360
Demo: https://eval.in/241781
Thanks for the help!

Cannot convert DateTime object to string

I am trying to generate date (after 7 months from now)
here is my code
$cdate = new DateTime("+7 months");
$cdate->modify("-" . ($cdate->format('j')-1) . " days");
$expiry_date= $cdate->format('Y-m-d');
$expiry_date = strtotime($expiry_date);
which gives the error:
PHP Catchable fatal error:
Object of class DateTime could not be converted to string
it is working with be before ... what is the problem??
DateTime class has no magic method __toString(), so you cannot use this object as string.
You should use getTimestamp()
$cdate = new DateTime("+7 months");
$expiry_date = $cdate->getTimestamp();
$cdate = new DateTime(+7 months);
$cdate = $cdate->format('Y-m-d');
Will cause cdate to be a string if that's what you are going for.

php date function returns a result I didn't expect

In my mysql db I got a timestamp field, the values are:
2013-01-30 01:15:00
2013-01-30 01:20:00
I use the date function to print it as 24HOUR:MINUTE format but I get a wrong time result.
mysql_query('SELECT time FROM locations ORDER BY time ASC');
...
echo "<td>" . date('H:i',$row['time']) . "</td>";
The output is: 00:33 for both records, why?
The second parameter for date() is a Unix timestamp.
Description
string date ( string $format [, int $timestamp = time() ] )
Returns a string formatted according to the given format string using
the given integer timestamp or the current time if no timestamp is
given. In other words, timestamp is optional and defaults to the value
of time().
echo "<td>" . date('H:i', strtotime($row['time'])) . "</td>";
An alternative solution is to use DateTime:
$datetime = new DateTime($row['time']);
echo $datetime->format('H:i');
// or in PHP5.5+
echo (new DateTime($row['time']))->format('H:i');
Reference:
date()
strtotime()
DateTime class

Add days & dispaly output in string

I need to add days to given string date and display calculated date in string
This is what I have tried, but I could not make it work.
$date = date_create('1-Feb-2012');
$newDate = date_modify($date, '+2 day');
echo 'Your date is' . $newDate . '.';
This gives an error
Object of class DateTime could not be converted to string
You need to tell the DateTime object how to format its output using DateTime::format. So, for example:
$date = new DateTime('1-Feb-2012');
$date->modify('+2 day');
echo 'Your date is' . $newDate->format('Y-m-d H:i:s') . '.';
Also note that modify directly modifies the DateTime - it doesn't just return a new one, as the documentation might lead you to believe - so I've removed the second variable. I've taken the liberty of changing the objects to the object-oriented form as well, which you should be using :)
Here's a working demo.
Use DateTime::format function.
usage:
echo 'Your date is' . $newdate->format('Y-m-d H:i:s') . '.';
or
echo 'Your date is' . date_format($newdate, 'Y-m-d H:i:s') . '.';

Categories