douxu4610 2017-04-19 04:35
浏览 39
已采纳

需要在php中按用户时区存储日期

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 2017-04-19 05:23
    关注

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

    <?php
    // here $usertimezone should be set = to what you have in your database
    $usertimezone="Asia/Shanghai"; 
    
    date_default_timezone_set('"'.$usertimezone.'"'); 
    
    //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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分