简体   繁体   中英

PHP date and time from an xml import

I have an xml file where the date stored in this format:

<sun rise="2014-05-30T02:51:30" set="2014-05-30T18:31:22"/>

My php process this value to a variable named $sunrise

2014-05-30T02:51:30

But my timezone is +2 so I have to add 2 more hours.

The only problem with this its just a string. I dont have any idea to how to convert it to date.

Since you already got the time inside that element (2014-05-30T02:51:30) you could just use strtotime() or alternatively, you could also use DateTime + DateInterval to add 2 more hours. Consider this example:

$sunrise = '2014-05-30T02:51:30';

$date = new DateTime($sunrise);
$date->add(new DateInterval('PT2H'));
echo $date->format('Y-m-d H:i:s');
// outputs: 2014-05-30 04:51:30

echo date('Y-m-d H:i:s', strtotime($sunrise . ' +2 hours'));
// outputs: 2014-05-30 04:51:30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM