我有
Fri Mar 13 2020 00:00:00 GMT+0100 (Ora standard dell’Europa centrale)-Mon Mar 23 2020 00:00:00 GMT+0100 (Ora standard dell’Europa centrale)
期待
Sardegna
13/03/2020-23/03/2020
但收到:
Sardegna
12/03/2020-22/03/2020
代码
$count = 0;
foreach($dates as $arr){
$exploded = explode('-',$arr);
$finalTime = '';
foreach($exploded as $explode){
$timeString = trim(explode('(',$explode)[0]);
$finalTime .= (!empty($finalTime)) ? "-".date('d/m/Y', strtotime($timeString)) : date('d/m/Y', strtotime($timeString));
}
echo "<li>".$nations[$count]."</li>";
echo "<li>".$finalTime."</li>";
echo PHP_EOL;
$count++;
}
答案 0 :(得分:1)
将字符串爆炸的内容从(
更改为' GMT'
$count = 0;
foreach($dates as $arr){
$exploded = explode('-',$arr);
$finalTime = '';
foreach($exploded as $explode){
$timeString = trim(explode(' GMT',$explode)[0]);
// ^^^^
// or as you dont need the trim any more
$timeString = explode(' GMT',$explode)[0];
$finalTime .= (!empty($finalTime)) ? "-".date('d/m/Y', strtotime($timeString)) : date('d/m/Y', strtotime($timeString));
}
echo "<li>".$nations[$count]."</li>";
echo "<li>".$finalTime."</li>";
echo PHP_EOL;
$count++;
}
这将删除时区,这是造成您的时差的原因
答案 1 :(得分:0)
快速/简单的答案是删除时区,以将其排除在等式之外:
// ...
$timeString = trim(explode('(',$explode)[0]); // (your code)
$timestring = str_replace("GMT+0100 (Ora standard dell’Europa centrale)", "", $timeString);
// ...
(这只是删除时区的许多非超级优雅方法之一,但应该证明这一点)