简体   繁体   中英

php Convert datetime to unixtime with timezone

I have datetime 2017-10-11 09:40:42 of Asia/Karachi I want to convert it to unix timestamp.

Note: I have multiple records with different timezone eg: Australia/Sydney

I am failed to convert it from following code

function unixTimefromTime($thetime='now',$timezone){
    if(is_numeric($thetime)){
        $dt = new \DateTime('@'.$thetime);
        $dt->setTimeZone(new \DateTimeZone($timezone));
        return $dt->getTimestamp();
    } else {
        $date = new \DateTime($thetime, new \DateTimeZone($timezone));
        return $date->getTimestamp();
    }
}

unixTimefromTime('2017-10-11 09:40:42','Asia/Karachi');
function DateTimeToUnixFromTimezone($dateNTime,$timezone){
    $date = new DateTime($dateNTime, new DateTimeZone($timezone));
    return $date->getTimestamp() + $date->getOffset();
}

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