Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
Difference between two times in seconds on carbon based on timezone

I want to find out the difference between 2 times for the different timezone. My server is based on Sydney and I want to find the difference between given time and current time(based on Perth) in seconds

    echo $tz = Carbon::now('Australia/Perth');
    echo "<br>";
    $local='2017-04-11 12:39:50';
    echo $emitted = Carbon::parse($local);
    echo "<br>";
    echo "diff from carbon->";
    echo $diff = $tz->diffInSeconds($emitted); 
    echo "<br> diff from Normal->";
    echo  $diff1 = strtotime($tz) - strtotime($emitted);

When I used diffInSeconds it gave 2 hours difference and looks like localisation is not taken consideration but strtotime($tz) - strtotime($emitted) gives perfect result. Any thing I missed?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have to tell Carbon which timezone is used for the string that should be parsed. The strtotime function is not the right choice for you I think, because it always consideres the parsed strings to be using the timezone from default_timezone_get()

For example:

    echo $now = Carbon::now('Australia/Perth');
    echo "<br>";
    echo $emitted = Carbon::parse($now, 'Australia/Sydney');
    echo "<br>";
    echo "diff from carbon->";
    echo $diff = $now->diffInSeconds($emitted);
    echo "<br> diff from Normal->";
    echo  $diff1 = strtotime($now) - strtotime($emitted);

Would result in :

diff from carbon->7200
diff from Normal->0

The normal diff is obviously wrong as $emitted is supposed to use 'Australia/Sydney' and $now is supposed to use 'Australia/Perth'. But as the two variables have the exact same string representation the diff is 0. (The information about the different timezones is lost).

The diff with Carbon however shows the correct difference of 7200 Seconds (= 2 hours ) which is the real difference between Australia/Sydney and Australia/Perth

By default all the date and date-time functions (including Carbon) are using the value from the timezone variable in your config/app.php file.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!