I am saving my timestamp in Laravel using the following.
Carbon::now()->timestamp;
How do you convert a timestamp into a specific time format with a specific timezone (using Carbon)?
$unix_time_stamp = "1572095520";
$date = Carbon::createFromTimestamp($unix_time_stamp);
$date->setTimezone('Pacific/Auckland')->format('Y-m-d H:i:s');
I am getting incorrect result on this one, should be Oct 27
Please try the following.
$unix_time_stamp = '1572095520';
$converted = Carbon::createFromTimestamp($unix_time_stamp,'Pacific/Auckland')
->toDateTimeString();
Go to config -> app.php and change 'timezone' => 'Pacific/Auckland',
Related
I have a UTC timestamp value 1615958170523 and I want to convert it into our local timezone.
I have tried this method:
The timestamp is in milliseconds that's why firstly I have converted in seconds and then used the below method.
$Date = date('m-d-Y H:i:s', 1615958170523/1000);
It always returns the time ~6hours ago i.e 03-17-2021 05:16:10 (Considering current time here), I don't want to add +5:30 hours to do the same.
Is it possible that we can use a standard method that means in-built functions which may be provided by Cakephp or PHP so that I can get the answer for the same?
I have also tried this one:
$gmtTimezone = new \DateTimeZone('GMT');
$myDateTime = new \DateTime(1615958170523/1000, $gmtTimezone);
It returns the same as I have used the date function.
You need to change the timezone after to define the timestamp in GMT.
$timestamp = 1615958170523/1000;
$myDateTime = \DateTime::createFromFormat('U', (int)$timestamp);
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 05:16:10
$myDateTime->setTimezone(new \DateTimeZone('Europe/Paris'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 06:16:10
$myDateTime->setTimezone(new \DateTimeZone('America/Denver'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-16 23:16:10
See DateTime::setTimezone() documentation
You should use the FrozenTime which will use the default Timezone you set in your config/app.php
I am from Philippines. If ever I will use Carbon::now(), it catches my machine time and not the UTC time.
My config/app.php is:
'timezone' => 'UTC',
This is my code:
$log->dateRequest = Carbon::now();
If ever I will post a request at 9:00pm (Philippine time). It catches 21:00:00 , instead of 13:00:00 (from UTC).
In one of you questions you mentioned that you need to use multiple timezones in your app. So, you can add timezone dynamically:
Carbon::now('UTC')
Or use setTimezone('UTC') method on existing date.
As stated in Carbon docs instantiation, try this:
$log->dateRequest = Carbon::now('UTC');
Carbon uses the default DateTime PHP object.
Gets the default timezone:
date_default_timezone_get();
// or
Carbon::now()->timezoneName;
Sets the default timezone
date_default_timezone_set('UTC');
var_dump(Carbon::now()->utc); // true;
you can try this
$log->dateRequest = Carbon::now('GMT+8')
try this
date_default_timezone_set('Asia/Manila');
$date = Carbon::createFromFormat('F j, Y g:i:a', date('F j, Y g:i:a'));
dd($date->format('F j, Y g:i:a')); // November 27, 2020 11:53:pm
you can also try this:
\Carbon\Carbon::now()->timezone('Asia/Manila')
I'm developing a PHP project and I'm using Parse SDK. What i want to do is adjust the time given by the Parse Database. It gives me time and date that 8 hours late to my timezone. Here's the code I'm using :
$query = new ParseQuery("TestObject");
$query->get("xWMyZ4YEGZ");
$dateTime = $query->getCreatedAt();
$sched = $dateTime->format("M d, Y - hA");
echo $sched;
How can i adjust it to specifically "GMT+8" TimeZone? Thanks!
You have to set the date_default_timezone_set before doing any thing as:
if(function_exists('date_default_timezone_set'))
date_default_timezone_set($timezone);
List of timezones are provided here...
Have a look at PHP date with TZ - See N.B.'s answer here is a code snippet he suggests (you may be able to use the parse date instead of the "now" parameter):
<?php
$tz = 'Europe/London';
$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('d.m.Y, H:i:s');
i am working on a Cakephp 2.x .. well the problem which i have has nothing to do this with cakephp .. so the scenario is i have a page called settings in which user sets his timezone according to his country and in database i am storing the time in gmt format in Userinfo table
so in userinfo table the gmt time is
5.00
now in my other tables i have a field called datetime in which i am storing the datetime in this format
2013-06-14 10:28:00
now on my view pages i want to display the data with dateTime of particular user ...
what i want is i want to add the gmt to this datetime so i can get the final datetime according to the user country ... hope you undertstand what i want to say...
Use Cake's build-in Timehelper. Add the Helper in your controller public $helpers = array('Time'); and then in your view:
$this->Time->format($format = NULL, $date, $default = false, $timezone = NULL)
In your case that'll be: $this->Time->format('d-m-Y H:i', $data['data']['datetime'], NULL, $data['User']['timezone']);
Good luck with that. :)
NOTE:
Since CakePHP 2.2 the order of $format and $date is changed. The way I explained is for CakePHP 2.2 or higher.
<?php
$utc = gmdate("M d Y H:i:s"); // place your gmt timestamp here
echo $utc."<br>";
$offset = date('Z'); //gets offset from gmt
echo $offset."<br>";
$localtime = strtotime($utc) + $offset; // adjusts to localtime.
echo date("M d Y H:i:s", $localtime);
?>
echo (new DateTime('2012-07-16 01:00:00 UTC'))
-> setTimezone(new DateTimeZone('GMT+8') )
->format('Y-m-d H:i:s');
will ouput
2012-07-16 09:00:00
thanks for reading.
Just need to know how i convert datetime gotten from my sql tables in gmtime to datetime in user timezone.
the following is my code but doesn't seem to work..
//WHERE $post_arr[5] is date from sql
$user_date=convert_date_for_user($post_arr[5]);
function convert_date_for_user($date_time){
$user = JFactory::getUser();
$db = JFactory::getDBO();
$timezone=$user->getParam('timezone');
echo $tz_offset;
$user_date = JFactory::getDate($date_time,$timezone);
$user_date_str = $user_date->toUnix(true);
return $user_date_str;
}
It converts but I'm getting all the wrong time from the above code.
The simplest way to do it:
$useUserTimeZone = true;
JHtml::date($sqlGmtTimestamp , 'D F n, Y', $useUserTimeZone);
$sqlGmtTimestamp takes GMT timestamp/datetime
$useUserTimeZone is a flag to use user's timezone, otherwise server's timezone will be used.
more details here: http://docs.joomla.org/API16:JHtml/date
You don't specify your Joomla version but, did you try Joomla's JDate class directly?
// Get the User and their timezone
$user = JFactory::getUser();
$timeZone = $user->getParam('timezone', 'UTC');
// Create JDate object set to now in the users timezone.
$myDate = JDate::getInstance('now', $timeZone);
// Gets the date as UNIX time stamp.
$myDate->toUnix():
// For your example using a method
function convert_date_for_user($date_time)
{
// Get the User and their timezone
$user = JFactory::getUser();
$timeZone = $user->getParam('timezone', 'UTC');
// Create JDate object set to now in the users timezone.
$myDate = JDate::getInstance($date_time, $timeZone);
return $myDate->toUnix();
}
This is the function that works for me:-
//WHERE date_time is the format of the date taken directly from database(ie: 0000-00-00 00:00:00)
function convert_time_zone($date_time){
$user =& JFactory::getUser();
$db = JFactory::getDBO();
$timezone=$user->getParam('timezone','UTC');
$time_object = new DateTime($date_time, new DateTimeZone('UTC'));
$time_object->setTimezone(new DateTimeZone($timezone));
$user_datetime=$time_object->format('Y-m-d H:i:s');
//SELECT ONLY 1 line below
return $user_datetime; //WOULD RETURN DATETIME IN 0000-00-00 00:00:00
//OR
return $time_object->getTimestamp(); //WOULD RETURN DATETIME IN UNIX TIMESTAMP
}
Its a little out of the way as i was hoping to use functions included in the joomla API to do it. If anyone could provide a better solution please do. and i select it as the right answer.
With Joomla 2.5+ (i think), you can use the following code
echo JHtml::_('date', $input, $format, $tz, $gregorian);
$input can be one of the following values:
"now" for the current time (DEFAULT)
A date/time string in a format accepted by date()
$format can be one of the following values:
NULL to use the default locale based format (DEFAULT)
A date format specification string (see http://php.net/manual/en/function.date.php)
$tz can be one of the following values:
TRUE to use the user's time zone (DEFAULT). Note: If the user's time zone is not set then the global config time zone is used.
FALSE to use global config time zone
NULL for no conversion
A timezone string (eg: "America/Los_Angeles", see http://php.net/manual/en/timezones.php)
$gregorian can be one of the following values:
TRUE to use Gregorian calendar
FALSE to NOT use Gregorian calendar (DEFAULT)
Having tried all the given possible solutions here and not getting the date in the user's timezone (Joomla! v.3.9.14), here's my (proven) solution:
$oUser_TZ = JFactory::getUser()->getTimezone();
$aUser_tz = (array)$oUser_TZ; // almost sure this step is not that necessary
$full_date = JFactory::getDate('now', $aUser_tz['timezone']); // pretty sure $oUser_tz->timezone will work
// I had try to use $full_date->Format('Y-m-d H:i:s') but it was giving me the non-converted-to-wanted-timezone date, so
$date_converted = substr($full_date, 0, 19);
date_converted gives me the date in format Y-m-d H:i:s and in the wanted timezone.
Try This:
$date = JFactory::getDate(); // now - 2014-03-11 08:45:22
$date->setOffset(8); // UTC+8
echo $date->toMySQL(); // wrong - 2014-03-11 08:45:22
echo '<br />';
echo $date->toFormat(); // right - 2014-03-11 16:45:22
JHtml::date($post_arr[5]);
If you want a different format, use the second parameter:
JHtml::date($post_arr[5], DateTime::RFC2822);
Which is equivalent to:
1. Create a JDate object with an UTC date read from the database
2. Get the correct Time Zone in Jomla Global Configuration and User Configuration
3. Call setTimeZone() to convert your JDate object to user local time
4. Call format() to format the JDate object as a well formatted string