Mukesh Khatri
Mukesh Khatri

Reputation: 73

How to change timezone of date and strtotime function in laravel

I have changed timezone with 'Asia/Kolkata' in 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',`

Now I trying to calculation between two time than output giving +5:30 time

how to fix this issue?

example:-

$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;

Output:-

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

I expecting "spent time format" given = 00:00:00

What can we do so that we can achieve this?

enter image description here

Thank You!

Upvotes: 1

Views: 135

Answers (1)

WanMin
WanMin

Reputation: 1

try to use Carbon in your prject.

$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');

Upvotes: 0

Related Questions