php - 使用 PHP 进行时间或时间间隔转换

标签 php time yii timezone timestamp

我在下拉列表中有以下给定的时间间隔选项(这些是字符串)

<select name="CSSAtapsClient[client_time_window][0]" id="client_time_window_0">
<option value="5702">7am - 10am</option>
<option value="5703">10am - 1pm</option>
<option value="5704">12pm - 3pm</option>
<option value="5705">3pm - 6pm</option>
<option value="5706">6pm - 9pm</option>
<option value="5707">7pm - 10pm</option>
<option value="5708">9pm - 12am</option>
<option value="5709">12am - 7am</option>
</select>

我需要将这些时间间隔转换为特定的 GMT 时区。例如,假设其时间为 GMT +8,我需要将其转换为 GMT +10,这可以通过增加 2 小时来完成。

因此,如果给定时间间隔为上午 7 点 - 上午 10 点 (GMT +8),则应为上午 9 点 - 中午 12 点 (GMT +10)

转换这种时间间隔的最佳方法是什么?我在这里看到的问题是它的字符串(时间间隔)。

感谢您的早日回复。

编辑 1

我仅转换为澳大利亚各州,因此转换时没有机会获得一天的差异。请检查此链接 http://www.timebie.com/tz/australiatimezone.php

最佳答案

试试这个功能。 (根据您的编辑,我将 gmt_to_gmt 函数的 $to_gmt var 设置为默认值“GMT +10”)

    <?php

        function is_am_pm($str){
            if(strpos($str, "am") !== false || strpos($str, "AM") !== false) return "am";
            if(strpos($str, "pm") !== false || strpos($str, "PM") !== false) return "pm";
        }
        function get_only_int($str){
            if(strpos($str, ".") !== false){
                if(preg_match_all("/\s(.*?)\.(.*?)$/", $str, $matches))
                    return trim(trim($matches[0][0]),"+");

            }else{
                return (int) preg_replace('/\D/', '', $str);
            }
        }
        function gmt_to_gmt($str,$from_gmt,$to_gmt = "GMT +10"){
            if(!is_numeric($from_gmt)) $from_gmt = get_only_int($from_gmt);
            if(!is_numeric($to_gmt)) $to_gmt = get_only_int($to_gmt);

            $temp_time = explode("-",$str);

            $begin_time_s   = is_am_pm($temp_time[0]);
            $begin_time     = get_only_int(trim($temp_time[0]));
            $end_time_s     = is_am_pm($temp_time[1]);
            $end_time       = get_only_int(trim($temp_time[1]));


            $time_diff  = $to_gmt - $from_gmt;

            $begin_time = $begin_time + $time_diff;
            $end_time   = $end_time + $time_diff;

            if($begin_time > 11){
                if($begin_time_s == "am"){
                    $begin_time >= 12 && $begin_time < 13 ? $begin_time : $begin_time -= 12;
                    $begin_time .= "pm";  
                }else{
                    $begin_time >= 12 && $begin_time < 13 ? $begin_time : $begin_time -= 12;
                    $begin_time .= "am";  
                }
            }else{
                $begin_time .= $begin_time_s;
            }

            if($end_time > 11){

                if($end_time_s == "am"){
                    $end_time >= 12 && $end_time < 13 ? $end_time : $end_time -= 12;
                    $end_time .= "pm";  
                }else{
                    $end_time >= 12 && $end_time < 13 ? $end_time : $end_time -= 12;
                    $end_time .= "am";  
                }
            }else{

                $end_time .= $end_time_s;
            }

            return $begin_time . " - " . $end_time;
        }
        echo gmt_to_gmt("11am - 10pm", "GMT +0" , "GMT +2.5"); 
    ?>

祝你有美好的一天

关于php - 使用 PHP 进行时间或时间间隔转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093165/

相关文章:

php - 如何获取网站上相对时间的MySQL表的时间戳

java - 如何在 Java 中计算时间跨度并格式化输出?

php - Yii2 - 如何强制响应 JSON 格式化程序使用对象而不是数组?

php - 如何在php中通过SSL连接Mysql数据库?

php - 哪个最不密集 - 在 PHP 或 MySQL 中计算唯一时间?

php - 正则表达式从具有至少一个数字的文本中提取所有单词

time - 显示视频的当前时间,而不是 videojs 上的剩余时间

javascript - 在 Controller 操作中调用 Javascript 函数 -YII

php - 如何处理在线状态。访客、用户等

php - php代码中的循环错误