2017-04-19 04:35 阅读 38


i have simple php script where i have this variable

$date = date('Y-m-d', time());

The Problem: The variable is storing date as per my server timezone.

What is want: I want to store date as per user time zone, take a look into

example below:

1- tom checkin from USA

2- jenne checkin from Asia since there is 12 hrs. difference so the date will be different too sometime

here is found some example but it's not dynamic Converting GMT time to local time using timezone offset in php

offset = '-0500';
$isDST = 1; // Daylight Saving 1 - on, 0 - off
$timezoneName = timezone_name_from_abbr('', intval($offset, 10) * 36, $isDST);
$timezone = new DateTimeZone($timezoneName);

Then you can use it in a DateTime constructor, e.g.

$datetime = new DateTime('2012-04-21 01:13:30', $timezone);

Now what exactly i am looking,

1- in case of TOM $date should be 18 11:38 PM Tuesday, 18 April 2017 (GMT-5) Time in Chicago, IL, USA

2- in case of jenne $date should be 19 9:40 AM Wednesday, 19 April 2017 (GMT+5) Time in Lahore

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dot_0620 dot_0620 2017-04-19 05:23

    difficult writing code in the comments section so i posted a working answer for you here

    // here $usertimezone should be set = to what you have in your database
    //new date and time
    $ndate= new datetime();
    //split into date and time seperate
    $nndate =$ndate->format("Y-m-d");
    $nntime= $ndate->format("H:i:S");
    //here you can test it
    echo $nndate;
    echo $nntime;
    点赞 评论 复制链接分享
  • dongluo6343 dongluo6343 2017-04-19 04:47

    Use this function date_default_timezone_set for setting timezone, From this function you can set the timezone according to user and then get the required format.


    date_default_timezone_set("new/timezone");//set the name of timezone here example Asia/Kokata
    echo $date= date("Y-m-d H:i:s");
    点赞 评论 复制链接分享