0

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');
user3151197
  • 347
  • 1
  • 3
  • 14
  • Try [this](https://stackoverflow.com/questions/1670797/convert-date-to-unixtime-php) – S4NDM4N Oct 11 '17 at 09:52
  • how its works with timezone.? – user3151197 Oct 11 '17 at 09:54
  • Can you please try this https://stackoverflow.com/questions/8668108/how-to-get-unix-timestamp-in-php-based-on-timezone – Abhijit Oct 11 '17 at 09:55
  • Also if you have done a search on google and here you would have found many answers. Even in PHP manual there are simple examples which you can work on [see the 3rd note](http://php.net/manual/en/datetime.gettimestamp.php) – S4NDM4N Oct 11 '17 at 10:03

1 Answers1

0
function DateTimeToUnixFromTimezone($dateNTime,$timezone){
    $date = new DateTime($dateNTime, new DateTimeZone($timezone));
    return $date->getTimestamp() + $date->getOffset();
}
user3151197
  • 347
  • 1
  • 3
  • 14
  • Worth noting that if you add the offset to a timestamp, it's no longer unix time. Doing this is almost always a mistake. I can't think of a reason why anyone would want this. – Evert Oct 11 '17 at 16:19