PHP code example of mbarlow / timezones

1. Go to this page and download the library: Download mbarlow/timezones library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mbarlow / timezones example snippets


    $timezones = new \MBarlow\Timezones\Timezones;

[
    /*
     * Package Service Providers...
     */    
    \MBarlow\Timezones\TimezonesServiceProvider::class,
];

use MBarlow\Timezones\Timezones;
class MyController 
{
    public function myControllerAction(Timezones $timezones)                
    {
        // 
    }
}

    $timezones = new \MBarlow\Timezones\Timezones;

    $dateTime = '2020-08-14 12:23:00';
    $tz = 'America/New_York';
    $format = 'Y-m-d H:i:s';

    $timezones->convertToUTC($dateTime, $tz, $format);

use MBarlow\Timezones\Timezones;
class MyController 
{
    public function myControllerAction(Timezones $timezones)                
    {
        $dateTime = '2020-08-14 12:23:00';
        $tz = 'America/New_York';
        $format = 'Y-m-d H:i:s';
    
        $timezones->convertToUTC($dateTime, $tz, $format);
    }
}

    $timezones = new \MBarlow\Timezones\Timezones;

    $dateTime = '2020-08-14 12:23:00';
    $tz = 'America/New_York';
    $format = 'jS F Y H:i';

    $timezones->convertToLocal($dateTime, $tz, $format);

use MBarlow\Timezones\Timezones;
class MyController 
{
    public function myControllerAction(Timezones $timezones)                
    {
        $dateTime = '2020-08-14 12:23:00';
        $tz = 'America/New_York';
        $format = 'jS F Y H:i';
    
        $timezones->convertToLocal($dateTime, $tz, $format);
    }
}

  $timezones = new \MBarlow\Timezones\Timezones;

  print_r($timezones->timezoneList());
  /*
    Array
    (
        [Pacific/Midway] => (UTC -11:00) Midway
        [Pacific/Niue] => (UTC -11:00) Niue
        [Pacific/Pago_Pago] => (UTC -11:00) Pago Pago
        [Pacific/Honolulu] => (UTC -10:00) Honolulu
        [Pacific/Rarotonga] => (UTC -10:00) Rarotonga
        [Pacific/Tahiti] => (UTC -10:00) Tahiti
        [Pacific/Marquesas] => (UTC -09:30) Marquesas
        [America/Adak] => (UTC -09:00) Adak
        [Pacific/Gambier] => (UTC -09:00) Gambier
        [America/Anchorage] => (UTC -08:00) Anchorage
        [America/Juneau] => (UTC -08:00) Juneau
        [America/Metlakatla] => (UTC -08:00) Metlakatla
        [America/Nome] => (UTC -08:00) Nome
        [Pacific/Pitcairn] => (UTC -08:00) Pitcairn
        [America/Sitka] => (UTC -08:00) Sitka
  
         ....
     )      
   */

$timezones = new \MBarlow\Timezones\Timezones;
$list = $timezones->timezoneList();

echo '<select="timezone">';
foreach ($list as $timezone => $label) {
    echo '<option value="' . $timezone . '">' . $label .  '</option>';
}
echo '</select>';