DateTime in php with timezone - php

I have a question. I try to use datetime in php.
I did :
$now = new \DateTime();
When I print_r the $now I have :
DateTime Object
(
[date] => 2016-12-01 05:55:01
[timezone_type] => 3
[timezone] => Europe/Helsinki
)
When I look at clock I have 16:05. I need to set the timezone ? I want to use Bucharest timezone. How I can get the right date and hour ? Thx in advance

You have two ways to set right timezone. It is object way and procedural way.
Examples
Object
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
Procedural
date_default_timezone_set("Europe/Bucharest");
$date = date('F d, Y H:i');
echo $date;
Manuals
PHP: date
PHP: DateTime
PHP: DateTimeZone
Update
Check code below, may it will work for you:
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
?>

There are examples in the manual, you can set the timezone on the instantiation of the DateTime class like this
$now = new \DateTime('now', new DateTimeZone('Europe/Bucharest'));

put this line of code above your script:
date_default_timezone_set('Europe/Bucharest');

<?php
$datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );
echo $datetime->format( 'Y-m-d H:i:s' );
Demo repl.it

You can use setTimezone() method of DateTime class to set the timezone to Europe/Bucharest, like this:
$now = new \DateTime();
$now->setTimezone(new DateTimeZone('Europe/Bucharest'));
Here's the reference:
http://php.net/manual/en/datetime.settimezone.php

Related

PHP date adding in DateTime object does not work

Try to do:
$datetime = new DateTime();
$onehour = new DateInterval('PT1H');
$datetime->add($onehour);
$timestamp = $datetime->date;
echo $timestamp; // nothing
What's wrong with it?
There's no such date property in the DateTime object.
Instead you can use format() to get the date formatted according to given format.
echo $datetime->format('Y-m-d H:i:s');
or use getTimestamp() to get the Unix timestamp:
echo $datetime->getTimestamp();
http://php.net/manual/en/datetime.format.php
http://php.net/manual/en/datetime.gettimestamp.php
You have to use :
DateInterval::createFromDateString("1 hours");
Instead of :
new DateInterval('PT1H');
As Given Below Code :
$onehour = DateInterval::createFromDateString("1 hours");
$date = new DateTime();
$date->add($onehour);
echo $date->format("Y-m-d H:i:s");

PHP MongoDate/DateTime bug or missunderstanding

Is there something wrong with the MongoDate feature?
When I convert a DateTime Object to MongoDate, and then I try to convert it back to DateTime, I get a totally different value. Keep in mind we're talking about a date within the EPOCH limits.
Here's a way to reproduce the issue.
$dateTime = new DateTime( '2015-07-20 10:15:45', new DateTimeZone( 'Europe/London' ) );
$mongoDate = new MongoDate( $dateTime->getTimeStamp() );
echo $mongoDate->sec ."\n"; // 1437383745
echo date( 'Y-m-d H:i:s', $mongoDate->sec ) ."\n"; // 2015-07-20 11:15:45
$dateTime2 = $mongoDate->toDateTime();
echo $dateTime2->format('Y-m-d H:i:s') ."\n"; // 1969-12-15 10:41:40
Is this behaviour normal?
You have timezone issue. When you create DateTime object, you create it in Europe/London timezone. But when you "convert" seconds back to datetime format, you use date() function, which uses default timezone, which apparently is different from Europe/London.
Create DateTime object with $mongoDate->sec and then convert timezone.
$dt = new DateTime('#' . $mongoDate->sec);
$dt->setTimezone(new DateTimeZone('Europe/London'));
echo $dt->format('c');

strtotime with variable using php - object given

This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks
You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;

Add hours to UTC dateTime

I am sure the answer is right in front of me, but I have a UTC dateTime that looks like this:
2014-01-02 04:02:58
All I want to do is add some hours to that.
How can I achieve this in PHP? Further, how would I add days to it if I wanted to?
Use DateTime(). Unlike date()/strtotime() it is timezone and daylight savings time friendly.
// PHP 5.2+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->modify('+2 hours');
echo $dt->format('Y-m-d H:i:s');
$dt->modify('+2 days');
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.3+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->add(new DateInterval('PT2H'));
echo $dt->format('Y-m-d H:i:s');
$dt->add(new DateInterval('P2D'));
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.4+
echo (new DateTime('2014-01-02 04:02:58'))->add(new DateInterval('PT2H'))->format('Y-m-d H:i:s');
See it in action
Reference:
DateTime()
DateInterval()
You can use strtotime() try like this :
echo $new_time = date("Y-m-d H:i:s", strtotime( "2014-01-02 04:02:58".'+3 hours'));

Php - Subtracting days from gmdate() date

I would like to subtract 180 days from a GMDATE() (for UTC purposes) I have in a PHP script. I have just looked on SO but hardly anybody is using gmdate().
This is what I have:
$from_date = gmdate("Y-m-d H:i:s");
Is there a simple way to subtract X amount of days from this, without using a different function completely? Or should I be doing it another way?
Use DateTime. Much better for working with dates especially when working with timezones.
$datetime = new DateTime(null, new DateTimeZone('UTC'));
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');
See it in action
Reference
DateTime
DateTimeZone
Formatted in UTC should be like;
$timezone = new DateTimeZone('UTC');
$datetime = new DateTime('NOW', $timezone);
$datetime->modify('-180 days');
echo $datetime->format('Y-m-d H:i:s');
Datetime: PHP DateTime class
public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
Timezone: PHP DateTimeZone class
public __construct ( string $timezone )
gmdate('Y-m-d H:i:s', time() - 15552000)

Categories