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条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题