How to convert DateTime in UTC format? - php

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

Related

Convert date time into universal timestamp in PHP for calendars

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

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!

DateTime data from XML convert to string

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

Using PHP to change the format of a MySQL date

I have dates stored in a MySQL database like so: 2012-02-10
When i output them using PHP, is there a function I can use that will output it like so 10/02/2012
Ive tried:
$theDate = date_format($row['date'], 'd/m/Y');
echo $theDate;
but it doesnt seem to work. Any help appretiated.
PHP Version 5.3.3
You need to use date_create() before using date_format(). This is because date_format() expects a DateTime object as the first parameter.
$date = date_create($row['date']);
echo date_format($date, 'd/m/Y');
Another way to do the same thing:
$dt = new DateTime('2012-02-10');
echo $dt->format('d/m/Y');
For the PHP 5.4 users out there it can be simplified to:
echo (new DateTime('2012-02-10'))->format('d/m/Y');
edit
To comment on the alternative solutions provided, they can be simplified to:
echo date('d/m/Y', strtotime($row['date']));
Just keep in mind that they do not account for daylight savings time or timezones like DateTime does.
The old way (non-OOP) to do it,
$t = strtotime('2012-02-10');
echo date('d/m/Y', $t);
Functions to check: strtotime, date
Here's a really simple way to do it
<?php
$theDate = $row['date'];
echo date("m/d/Y", strtotime($theDate));
?>
Here are a few examples:
$input = '2012-02-10';
// datetime (object oriented style)
$dt = new DateTime($input);
echo $dt->format('d/m/Y') . "\n";
// datetime (procedural style)
$dt = date_create($input);
echo date_format($dt, 'd/m/Y') . "\n";
// strtotime
$m = strtotime($input);
echo date('d/m/Y', $m) . "\n";
// substr
echo substr($input,8,2) . "/" . substr($input,5,2) . "/" . substr($input,0,4) . "\n";
// explode
$m = explode('-', $input);
echo "$m[2]/$m[1]/$m[0]" . "\n";
// preg_match
preg_match('~^(\d+)-(\d+)-(\d+)$~', $input, $m);
echo "$m[3]/$m[2]/$m[1]" . "\n";
// sscanf
$m = sscanf($input, '%d-%d-%d');
echo "$m[2]/$m[1]/$m[0]" . "\n";
p.s. did I miss any? ;)

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