Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set time zone with DateTime object?

Tags:

date

php

datetime

I made a code that print a log in a server folder, this log return also a date with hours but the server isn't in my Nation so the hours is incorrect for me. Actually this is my code:

$now = new DateTime();  
$aggiornata = "\r\n" . "Aggiornamento eseguito con successo per: " . $value['caption'] . " DATA: " . $now->format('d-m-Y H:i:s');
file_put_contents(getenv('OPENSHIFT_REPO_DIR').'php/log.txt', $aggiornata, FILE_APPEND);

How to set the correct hour for Italy?

like image 974
Salvatore Fucito Avatar asked Aug 09 '15 08:08

Salvatore Fucito


2 Answers

look at DateTimeZone

$date = new DateTime('now',new DateTimeZone("Europe/Rome"));
like image 100
Daniel Krom Avatar answered Oct 04 '22 23:10

Daniel Krom


You can also do it at the top of of your file like for example:

date_default_timezone_set('Europe/Rome');

like image 30
Ali Gajani Avatar answered Oct 04 '22 23:10

Ali Gajani