php 如何在laravel中更改日期和strtotime函数的时区

xfb7svmp  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(97)

我在app.php中将时区更改为“亚洲/加尔各答”

Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Asia/Kolkata',`

现在我尝试计算两个时间之间的比输出给出+5:30时间
如何解决此问题?
例子:-

$inTime = "10:08:00";
$outTime = "10:08:00";
$spentTime = strtotime($outTime) - strtotime($inTime);
echo "<pre>";
print_r("out time = ".$outTime);
echo "<br>";
print_r("in time = ".$inTime);
echo "<br>";
print_r("spent time = ".$spentTime);
                
$spentTime = **date('H:i:s', $spentTime)**;
echo "<br>";
print_r("spent time format = ".$spentTime);
die;

输出:-

out time = 10:08:00
 in time = 10:08:00
 spent time = 0
 spent time format = 05:30:00

我期望"花费时间格式"给定= 00:00:00
我们可以做些什么来实现这一目标?

谢谢你!

pes8fvy9

pes8fvy91#

试着在你的项目中使用碳。

$inTime = Carbon::parse('10:08:00', 'Asia/Kolkata');
$outTime = Carbon::parse('10:08:00', 'Asia/Taipei');
//You can replace diffInSeconds with other diff methods. 
$spentTime = $outTime->diff($inTime)->format('%H:%I:%S');

相关问题