How can I get the current date and time for this time zone 'America/phoenix'? I can't figure out how this can be done with the DateTime::createFromFormat.
My Code example
<?php
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, '/*Example: 6-12-2019 12:05am*/', new
DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
?>
You can change the second parameter of createFromFormat, but from your question, I guess you are not America/phoenix, therefore, use this code:
<?php
date_default_timezone_set('America/phoenix');
$currentTimeInPhoenix = date('n-j-Y g:iA');//gets the currenttime in phoenix
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, $currentTimeInPhoenix, new DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
date_default_timezone_set('YOUR OLD TIMEZONE');
?>
Related
I am using yii framework
I am using below to code to get time
<?php echo date('Y/m/d H:i:s') ?>
how can i current time of UAE?
Try this:
$tz = 'Asia/Dubai'; // your required location time zone.
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('Y/m/d H:i:s');
Use date_default_timezone_set() :
date_default_timezone_set('Asia/Dubai');
echo "The time is " . date('Y/m/d H:i:s');
In my postgresql, the I have the following column named "created" that has the type timestamp with timezone
So I inserted the record according to the format as such which I believe is UTC.
2015-10-02 09:09:35+08
I am using php Carbon library so i did the following:
$date = Carbon\Carbon::parse('2015-10-02 09:09:35+08');
echo $date->->toDatetimeString();
//gives result as 2015-10-02 09:09:35
How can I use the library to echo the correct timezone which includes the adding of the +8 in the above datetime format? The timzezone that I am using is "Asia/Singapore".
The time should be printed to local timing which is 2015-10-02: 17:09:35:
Try this:
$timestamp = '2015-10-02 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Asia/Singapore');
Try this using standard PHP:
$raw = '2015-10-02 09:09:35+08';
$date = substr($raw,0,19);
$tzOffset = (strlen($raw) > 19) ? substr($raw,-3) : 0;
$timestamp = strtotime($date) + (60 * 60 * $tzOffset);
$localTime = date('Y-m-d H:i:s',$timestamp);
echo 'local time:['.$localTime.']';
The result is:
local time:[2015-10-02 17:09:35]
This will also work without a time zone offset or a negative one.
You can do this using native php without using Carbon:
$time = '2015-10-02 16:34:00+08';
$date = DateTime::createFromFormat('Y-m-d H:i:s+O', $time);
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Asia/Singapore'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Etc/UTC'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
Guys I am trying to get correct Indian time and date in PHP
I have tried this code:
$date = date("d/m/Y");
$date1 = date("H:i a");
if(function_exists('date_default_timezone_set'))
{
date_default_timezone_set("Asia/Kolkata");
}
echo $date;
echo $date1;
But I am not getting correct time. I am getting time which is 4.30 Hours late. Is there any mistake in my code?
Put the timezone declaration first before using any date function:
// set the timezone first
if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}
// then use the date functions, not the other way around
$date = date("d/m/Y");
$date1 = date("H:i a");
echo $date . '<br/>';
echo $date1;
The cleaner option is
$date = new DateTime(null, new DateTimezone("Asia/Kolkata"));
echo $date->format('d/m/y').'<br/>';
echo $date->format('H:i a');
This way, you wouldn't alter global state, so other pieces of the code can still use other timezones
<?php
date_default_timezone_set("Asia/Kolkata");
echo "The date is " .date("d/m/y");
echo "<br/>";
echo "The time is " . date("h:i:sa");
?>
I think you should try this
date_default_timezone_set('Asia/Kolkata');
and
$date = date('Y-m-d H:i:s');
what ever format you like!!
// set the timezone first
if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}
// then use the date functions, not the other way around
$date = date("d/m/Y");
$date1 = date("H:i a");
echo $date . '<br/>';
echo $date1;
In my PHP program, I'm using $_SERVER to log the page's date visited:
$dateStamp = $_SERVER['REQUEST_TIME'];
The result is that the $dateStamp variable contains a Unix timestamp like:
1385615749
What's the simplest way to convert it into a human-readable date/time (with year, month, day, hour, minutes, seconds)?
This number is called Unix time. Functions like date() can accept it as the optional second parameter to format it in readable time.
Example:
echo date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
If you omit the second parameter the current value of time() will be used.
echo date('Y-m-d H:i:s');
Your functional approch to convert timestamp into Human Readable format are as following
function convertDateTime($unixTime) {
$dt = new DateTime("#$unixTime");
return $dt->format('Y-m-d H:i:s');
}
$dateVarName = convertDateTime(1385615749);
echo $dateVarName;
Output :-
2013-11-28 05:15:49
Working Demo
<?php
$date = new DateTime();
$dateStamp = $_SERVER['REQUEST_TIME'];
$date->setTimestamp($dateStamp);
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
you can try this
<?php
$date = date_create();
$dateStamp = $_SERVER['REQUEST_TIME'];
date_timestamp_set($date, $dateStamp);
echo date_format($date, 'U = D-M-Y H:i:s') . "\n";
?>
this code will work for you
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d-M-Y H:i:s',strtotime($dateStamp));
REQUEST_TIME - It is unix timestamp - The timestamp of the start of the request.
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d m Y', $dateStamp);
OR
$date = new DateTime($dateStamp);
echo $date->format('Y-m-d');
I'm receiving some data from an HTTP POST which includes what is labelled a GMT Timestamp:
<gmt_timestamp>201308031525</gmt_timestamp>
I then need to take this timestamp and convert it to this format:
MM/DD/YYYY HH:MM
So far I've been trying this:
$ts = $_GET['timestamp'];
$date = DateTime::createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
but that generates a "PHP Fatal error: Call to a member function format() on a non-object" for the 2nd line. Any idea what I'm doing wrong?
You have a bug in this line:
$date = DateTime::createFromFormat('ymdHi', $ts);
You need an uppercase Y for the year:
$date = DateTime::createFromFormat('YmdHi', $ts);
A lowercase y indicates "A two digit representation of a year", whereas you need Y ("A full numeric representation of a year, 4 digits"). See the docs here.
You also need to set the timezone before you begin:
date_default_timezone_set('UTC');
(PHP does have a GMT timezone, but it shouldn't be used. UTC behaves the same as GMT within PHP.)
Edit
To get your desired output format of:
MM/DD/YYYY HH:MM
you need to do:
$fmTimestamp = $date->format('m/d/Y H:i');
Also, since you're "receiving some data from an HTTP POST", you need to use $_POST instead of $_GET:
$ts = $_POST['timestamp'];
So the complete code is:
date_default_timezone_set('UTC');
$ts = $_POST['timestamp'];
$date = DateTime::createFromFormat('YmdHi', $ts);
$fmTimestamp = $date->format('m/d/Y H:i');
Keep it simple stupid.
$input = $_GET['timestamp']; // 201308031525
$year = (int)substr($input,0,4);
$month = (int)substr($input,4,2);
$date = (int)substr($input,6,2);
$hour = (int)substr($input,8,2);
$minute = (int)substr($input,10);
$date_obj = new DateTime($year . '-' . $month . '-' . $date .' ' . $hour . ':' . $minute);
echo $date_obj->format('m/d/Y h:i:s A');
and the output is:
08/03/2013 03:25:00 PM
You are not instantiating the object you are trying to use.
Try this approach instead:
$date = new DateTime;
$date->createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
This is untested, just saying ...