The user creates date in format like as: 12/23/2016 22:10
After this data should be sent convert to another timezone, for example GMT + 2:
12/23/2016 00:10
I can assume the date that I create should be saved in GTM 0, only after I can change timezone.
How can I use this mechanism in Laravel?
You can use Carbon to change the the timezone of a date as:
$date = Carbon\Carbon::parse('12/23/2016 22:10', 'GMT');
And to add +2 you can do as:
$date->addHours(2)
You can change the timezone for your application in: /config/app.php,
The default timezone is UTC. You can find all supported timezones here: http://php.net/manual/en/timezones.php
Mostly I'm agree with Amit Gupa answer, but the best way to manage timezone conversion isn't by adding hours, but by converting it on Carbon:
$yourdate->timezone('somecountrytimezone')
This way you won't have to worry about different time management between countries (Like daylight saving time) and just manage to get your works done.
Please refer do the documentation : http://carbon.nesbot.com/docs/#api-settersfluent
We can use parse and createFromFormat methods of Carbon to set our datetime as a certain timezone then use setTimezone to convert it to another timezone.
$date = "2022-08-05 12:00:00"; //set datetime for checking
$now = Carbon::now()->timezone('Europe/London')->format('Y-m-d H:i:s');
$createFrom = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'Asia/Hong_Kong')->format('Y-m-d H:i:s');
$parse = Carbon::parse($date,'Asia/Hong_Kong')->setTimezone('Europe/London')->format('Y-m-d H:i:s');
$converted = Carbon::parse($date,'Asia/Hong_Kong')->timezone('Europe/London')->format('Y-m-d H:i:s');
outputs: when dd($now,$createFrom,$parse,$converted);
^ "2022-08-05 08:33:48"
^ "2022-08-05 12:00:00"
^ "2022-08-05 05:00:00"
^ "2022-08-05 05:00:00"
that's not related to laravel , it's just a php , so you can simply get the time-zone offset (exp GMT+3) then do like this :
$timeZoneOffset ---> for GMT+3 you put (3)
function getNewDateByTimeZone($date, $timeZoneOffset)
{
/* if the client Time zone is GMT */
if (!$timeZoneOffset || $timeZoneOffset == 0) {
$timeZoneOffset = "-0";
}
$op = $timeZoneOffset[0];
$timeZoneOffset = preg_replace('/[^0-9.]+/', '', $timeZoneOffset) * 60;
$userDate = date($date, time());
$timeStamp = strtotime($userDate);
$offsetTime = $op == '-' ? $timeStamp + $timeZoneOffset : $timeStamp - $timeZoneOffset;
return date("Y-m-d H:i", $offsetTime);
}
so like that you will save the date in GMT-0
Related
I want to change the date format to the Indonesian date format.
I have added code like this in app/Providers/AppSserviceProvider.php
public function boot()
{
config(['app.locale' => 'id']);
\Carbon\Carbon::setLocale('id');
}
and I call him a command like this:
\Carbon\Carbon::setLocale('id');
echo \Carbon\Carbon::now()->format('l, d F Y');
but it did not work well, the date format was not yet Indonesian. What is wrong ?
You can control timezone of your application in the timezone variable inside config/app.php file.
Don't foget to clear cache after: php artisan config:cache.
upd: try this:
setlocale(LC_ALL, 'IND');
echo \Carbon\Carbon::now()->formatLocalized('%A %d %B %Y');
Silakan dicoba kawan :
$date = Carbon::parse('2021-03-16 08:27:00')->locale('id');
$date->settings(['formatFunction' => 'translatedFormat']);
echo $date->format('l, j F Y ; h:i a'); // Selasa, 16 Maret 2021 ; 08:27 pagi
You can control timezone of your application in the timezone variable inside config/app.php file. Don't foget to clear cache after: php artisan config:clear.
and try this in app.php:
'timezone' => 'Asia/Jakarta'
then your carbon like this:
Carbon::now()->formatLocalized('%A, %d %B %Y')
in my blade there is a DateTime picker which selects in mm/dd/yyyy hh:mm AM/PM format. and i need to extract this to date = 8/29/2011 and time = 13:00 .
how can I do that?
Do as below.
$stringdate = "8/29/2011 1:00 PM";
$timestemp = strtotime($stringdate);
$date = date('Y-m-d', $timestemp);
$time = date('H:i:s',$timestemp);
echo $date;
echo $time;
check example
Edit:- as you want this format YYYY-MM-DD hh:mm
$datetime = date('Y-m-d H:i', strtotime("8/29/2011 1:00 PM"));
echo $datetime; //output "2011-08-29 13:00";
Try this way.
#php
$input = '2017-06-27 12:58:20';
$format1 = 'Y-m-d';
$format2 = 'H:i:s';
$date = Carbon\Carbon::parse($input)->format($format1);
$time = Carbon\Carbon::parse($input)->format($format2);
#endphp
It is simple just try it
$date="8/29/2011 1:00 PM"; // date that get from view (blade page)
$createTimestamp = new DateTime($date); // create timestamp object from string
$date = $createTimestamp->format('m/d/Y'); // get date
$time = $createTimestamp->format('H:i:s A'); // get time with AM/PM format
return "date: ".$date." time: ".$time; // return your result
Try this way
Use Carbon;
$dateAndTime=Carbon::parse('8/29/2011 1:00 PM');
$date=$dateAndTime->format('d-m-Y');// 29-8-2011
$time=$dateAndTime->format('H:i:s');// 1:00
$time=$dateAndTime->format('H:i:s A');// 1:00 AM/PM
Refer this link to understand more about Carbon.
I have been trying for a while for saving time in my database in timeformat like 10:00:00.
What I want is simply pick up time from timepicker and save data(time) in timeformat.
Here is what I have done:
I have used timepicker as it gets data in format 10:52 AM.
I have used accessor methods to save time as follows:
public function setRTimeAttribute($value)
{
$this->attributes['r_time'] = date('h:i:A', strtotime($value));
}
My post controller in case needed as follows:
public function postSessionReservation(Request $request,$profile_slug)
{
$restaurant = Restaurant::where('slug_profile', $profile_slug)->select('name','ar_name','slug_profile','id')->firstOrFail();
$this->validate($request,[
'no_of_seats'=>'required',
'r_date'=>'required',
'r_time'=>'required',
'restaurant_id'=>'required',
]);
$data = [
'no_of_seats'=>$request->no_of_seats,
'r_date'=>$request->r_date,
'r_time'=>$request->r_time,
'restaurant_id'=>$request->restaurant_id
];
$minutes = 1;
Session::put('reservation',json_encode($data),$minutes);
return redirect($restaurant->slug_profile.'/complete-reservation');
}
But it saves the time as 12:00:00 each time.
I do not know how 12:00:00 is generated whenever I save the time. I tried a lot but could not figure out.
Hope you guys will help me solve this.
Thanks in advance.
First remove spaces between time "10 : 52 AM" to "10:52 AM" and then change your line to this:
$this->attributes['r_time'] = date('H:i', strtotime( $value ));
This will surely help, I already tested it.
For example you can remove spaces through:
$a = '10 : 52 PM';
$x = preg_replace('/\s*:\s*/', ':', $a);
date("H:i", strtotime($x));
I have converted date to my local time as below:
$this->date_string = "%Y/%m/%d %h:%i:%s";
$timestamp = now();
$timezone = 'UP45';
$daylight_saving = TRUE;
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$this->updated_date = mdate($this->date_string,$time);
And I'm storing this field in to database.
Now at retrieval time I want format like this:
"11-04-2011 4:50:00 PM"
I have used this code:
$timestamp = strtotime($rs->updated_date);
$date1 = "%d-%m-%Y %h:%i:%s %a";
$updat1 = date($date1,$timestamp);
But this will give me only
"11-04-2011 4:50:00 AM"
But I have stored it like it was PM.
Might get voted down, but will have a go at it.
Is it because the MySQL stores it in 24 hour format? (assuming you are using the datetime field type)
Maybe this will help
Converting mysql TIME from 24 HR to AM/PM format
sorry if it doesn't.
I have a problem with one field of the DB. With this code:
$expireMonth = Mage::getStoreConfig('points_options/config_points/expiration_period', Mage::app()->getStore()->getId());
if (!is_null($expireMonth) && ($expireMonth > 0)) {
$expireDate = date("Y-m-d H:i:s", strtotime("+" . $expireMonth . " month"));
} else {
$expireDate = NULL;
}
//die($expireDate);
//store in points history table
$this->_pointsModel->setCustomerId($this->_customer->getId())
->setOrdersId('welcome')
->setPointsPending($pointsForNewCustomer)
->setPointsComment(Mage::helper('points')->__('welcome points'))
->setDateAdded(date('Y-m-d H:i:s'))
->setPointsStatus(2)//confirmed
->setPointsType('WE')
->setStoreId(Mage::app()->getStore()->getId())
->setExpireDate($expireDate)
->save();
Every field is saved in the table, except for expire_date. If I uncomment the die($expireData), I see the correct value, something like 2012-01-13 13:21:12. The field is defined as:
`expire_date` datetime NULL
Any thoughts?
edit: The solution is:
$expireDate = date("Y-m-d H:i:s", strtotime("+" . $expireMonth . " months"));
Check out the "s" in my strtotime expression.
I know many attributes allow for some sort of formatting before writing and after reading. Have you tried setting the value as a unix timestamp instead?