// Date and time in php
// Create source date/time variables
$sDateTime = '26 Aug 2018 06:30:01';
$sLocalTZ = 'America/New_York';
$iYear = 2018;
$iMonth = 8;
$iDay = 26;
$iHour = 6;
$iMin = 30;
$iSec = 1;
/////////////////////////////////////////////////////////////////////////////////////
// Time zone (always know or establish the relevent timezone)
if (date_default_timezone_set($sLocalTZ))
echo 'Timezone is '.date_default_timezone_get().'
';
else
echo 'ERROR - unable to change the default timezone to '.$sLocalTZ.'
';
if (date_default_timezone_set('UTC') == false) die('ERROR - unable to set default timzeone to UTC');
/////////////////////////////////////////////////////////////////////////////////////
// Creating datetime
// Create unix timestamp (seconds since January 1 1970 00:00:00 UTC) relative to the current time zone
$iUnixTime = mktime($iHour,$iMin,$iSec,$iMonth,$iDay,$iYear);
// Create datetime object from datetime string in the UTC time zone
if (date_create($sDateTime) == false) die('ERROR - date_create()');
$oDateTime = date_create($sDateTime);
/////////////////////////////////////////////////////////////////////////////////////
// Datetime conversions
// Convert datetime string to unix timestamp (seconds since January 1 1970 00:00:00 UTC)
// Conversion is relative to the current time zone.
if (strtotime($sDateTime) == false) die('ERROR - conversion of date failed '.$sDateTime);
$iUnixTime = strtotime($sDateTime);
// Convert unix timestamp to datetime string
$iUnixTime = mktime($iHour,$iMin,$iSec,$iMonth,$iDay,$iYear);
$sUnixTime = date('j M Y h:i:s A', $iUnixTime);
//echo '$sUnixTime = '.$sUnixTime.'
'; // output: "$sUnixTime = 26 Aug 2018 06:30:01 AM"
unset($sUnixTime);
// Convert datetime string to another time zone (UTC to 'America/New_York')
// (Note that the datetime string is derived from the unix timestamp $iUnixTime using the date() function).
$dtLocal = date_tz_convert(date('d/m/Y H:i:s',$iUnixTime),'UTC','d/m/Y H:i:s',$sLocalTZ, 'jS M Y h:i a');
// echo '$dtLocal = '.$dtLocal.'
'; outputs "26th Aug 2018 02:30 am"
/////////////////////////////////////////////////////////////////////////////////////
// Displaying datetime
// Use date() to display a Unix timestamp
$iUnixTime = mktime($iHour,$iMin,$iSec,$iMonth,$iDay,$iYear);
echo '$iUnixTime = '.date('j M Y h:i:s A', $iUnixTime).'
';
// Use date_format() to display a datetime object
$oDateTime = date_create($sDateTime);
echo '$oDateTime = '.date_format($oDateTime,'j M Y h:i:s A').'
';
/////////////////////////////////////////////////////////////////////////////////////
// Datetime manipulations
// Add one day to datetime variable $startDT
if (date_add($oDateTime, date_interval_create_from_date_string('3 days')) == false) die('ERROR - date_add()');
//echo '$oDateTime = '.date_format($oDateTime,'j M Y h:i:s A').'
'; // outout "$oDateTime = 29 Aug 2018 06:30:01 AM"
/////////////////////////////////////////////////////////////////////////////////////
// Custom datetime functions
function date_tz_convert($dt, $tz1, $df1, $tz2, $df2) {
/**
* Convert a date(time) string to another format or timezone
*
* DateTime::createFromFormat requires PHP >= 5.3
*
* @param string $dt
* @param string $tz1
* @param string $df1
* @param string $tz2
* @param string $df2
* @return string
https://www.pontikis.net/tip/?id=29
*/
$res = '';
if(!in_array($tz1, timezone_identifiers_list())) { // check source timezone
trigger_error(__FUNCTION__ . ': Invalid source timezone ' . $tz1, E_USER_ERROR);
} elseif(!in_array($tz2, timezone_identifiers_list())) { // check destination timezone
trigger_error(__FUNCTION__ . ': Invalid destination timezone ' . $tz2, E_USER_ERROR);
} else {
// create DateTime object
$d = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1));
// check source datetime
if($d && DateTime::getLastErrors()["warning_count"] == 0 && DateTime::getLastErrors()["error_count"] == 0) {
// convert timezone
$d->setTimeZone(new DateTimeZone($tz2));
// convert dateformat
$res = $d->format($df2);
} else {
trigger_error(__FUNCTION__ . ': Invalid source datetime ' . $dt . ', ' . $df1, E_USER_ERROR);
}
}
return $res;
}
//
// ===========================================================================
//
// MIT License
//
// Copyright (c) 2018 Mechatronic Solutions LLC (www.http://mechatronicsolutionsllc.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ===========================================================================
Sitemap | Copyright © 2017 - 2025 Mechatronic Solutions LLC
Web site by www.MechatronicSolutionsLLC.com | | 13.3260 ms