I have a query for time and display it in a pdf, but it is form to string format i want it to convert it to 12 format time.
This is my codes {nl2br($schedule->cattime)}
laravel is using Carbon as a date/time layer.
to change time/date format in carbon, you need to use createFromFormat method.
Carbon::createFromFormat($format, $time, $tz);
for example, to get 12h time format:
$date = Carbon::createFromFormat('m/d/Y g:ia', '03/10/2017 20:10');
to use carbon from your blade template directly , you will need to use the full namespace:
\Carbon\Carbon::createFromFormat('m/d/Y g:ia', '03/10/2017 20:10');
Update
depending on your comment, you are getting values from database.
it would be easier to fetch formatted time rather than re-format it using TIME_FORMAT function
selectRaw('GROUP_CONCAT(concat(TIME_FORMAT(schedule.start_time, "%r")) separator "\r\n") as cattime')
Related
I have a lot of variables of dates and times and I need to setTimezone in an abstract file. So I don't know anything about what is my times or dates format.
How I can keep the same format after changing the timezone? Or is there any way to find what is my date and time format?
For example, I have a timestamp like Y-m-d H:i and in my abstract class I'm changing the time zone by Carbon::parse($timestamp)->setTimezone($userTimezone) but with this line of code I'm losing my timestamp format and this code should convert multi-format of date and times. Another timestamp may have a format like Y/m/d H:I:s
I need something like this:
$format = Carbon::parse($timestamp)->getFormat(); // Returns 'Y-m-d H:i'
Or this can help; How I can setTimezone of a string timestamp without changing the format? date_default_timezone_get and date_default_timezone_set is not a good idea for this question.
You can simple change to:
Carbon::parse($timestamp)->setTimezone($userTimezone)->format('Y-m-d H:i');
I'm using laravel 5.5 and Carbon library to write an API.
I store all date and time values in MySQL database as common YYYY-MM-DD HH:MM:SS format.
But on the other hand front-end developer ask me to return all dates as 2017-12-20T20:30:00.000Z. seems that is a common format in JavaScript.
Is there any way to convert all DateTime formatted fields to desired format via laravel or Carbon ?
Update:
First problem is solved but another one is that client want to send date times as same 2017-12-20T20:30:00.000Z for all fields of table. while I should get and save them as common DateTime. What can I do in this case?
Javascript uses ISO 8601 syntax. In PHP, you can use like this:
date('c', strtotime($yourDateTime)); // c is ISO 8601 format
Reference here:
Since the date is casting as Carbon, use format():
$date->format($desiredFormat);
The list of characters for creating $desiredFormat will also be helpful.
Laravel Provide getter and setter method for format Eloquent attribute values
by default laravel return 'dateTime' as carbon instance
so, you can change format in model using getter method
public function getDateTimeAttribute($value)
{
$desiredFormat = "Y-M-d"; //change format as per your requirement
$value->format($desiredFormat);
}
Link for more about getter and setter method
https://laravel.com/docs/5.5/eloquent-mutators
{{ date('M j, Y h:ia', strtotime($created_at)) }}
It gives us like "Dec 30, 2017 16:39am"
You can edit you date time of course(M j, Y h:ia) just google it "php: date - manual"
I am working on some data migration process, with consists of loads of CSV's
The problem I'm facing now is that the data file I am working with has 2 columns, namely "date" and "time". Now, date itself doesn't have a problem. My "time" column can come in any format, i.e. H:i, or H:i:s. Regardless of the format, I want my output to be d/m/Y H:i:s, and to just append 00 if seconds is not given.
Carbon's createFromFormat requires me to specify what my input format is, which I can't. Currently what I am doing is using Excel to split my CSV files into different files according to the time format, then import them separately, but it's taking too much time.
I can't seem to find a function that allows me to do this either:
if(getSecondsFromTime){
Carbon::createFromFormat($string, 'd/m/Y H:i:s');
}else{
Carbon::createFromFormat($string, 'd/m/Y H:i');
}
Does Carbon/PHP Datetime has a function that I am unaware of that can do this? Or do I have to resolve to regex to do what I need?
If you replace your / with - then Carbon::parse will work:
$t = '05/10/2017 18:00';
$c = Carbon::parse(str_replace('/', '-', $t));
I have a controller action in which I fetch the currrent date like following:
$fullDate=date("Y-m-d");
$date = strtotime($fullDate);
$viewModel->setVariable("currentDate",$date);
// Here I would like to pass the variable to the View and then compare it with another date ...
With the method above I'm getting this when I do var_dump($currentDate);
int(1463587200)
which is not what I was expecting... I need a date in format like this 2016-05-19 (year-month-day)... How can I do this the valid way???
This strtotime is converting your formatted date into a Unix time-stamp. just remove it.
$fullDate=date("Y-m-d");
$viewModel->setVariable("currentDate",$fullDate);
you'll be able to compare two dates in this format Y-m-d without converting to time-stamp.
I have this code:
$dateTime = new DateTime('#'.strtotime('+30 minutes'));
$dateTime->setTimezone(new DateTimeZone($someModel->timezone));
$otherModel->send_at = $dateTime->format($otherModel->getDateTimeFormat());
Where $otherModel->getDateTimeFormat() returns M/d/yy h:mm a which is fine because it is based on the current Yii locale which is based on CLDR as far as i know.
Now, when i pass this format to PHP's DateTime::format() class method [$dateTime->format($otherModel->getDateTimeFormat())] i get this result: Dec/06/1313 03:1212 pm which is looking weird because the format that php accepts for date/datetime is not the same as the one Yii is using in it's locales.
How should one fix such issue?
This is the fix:
$dateTime = new DateTime('#'.strtotime('+30 minutes'));
$dateTime->setTimezone(new DateTimeZone($someModel->timezone));
// get a timestamp from the current date that also knows about the offset.
$timestamp = CDateTimeParser::parse($dateTime->format('Y-m-d H:i:s'), 'yyyy-MM-dd HH:mm:ss');
// now format using Yii's methods and format type
$otherModel->send_at = Yii::app()->dateFormatter->formatDateTime($timestamp, 'short', 'short');
The idea is to use the PHP's DateTime::format() method to extract the timestamp that has taken into consideration the user timezone. Then, based on this timestamp, format according to Yii datetime formatting.
Well, nothing strange, your date format is M/d/yy h:mm a, and according to DateTime::format() documentation :
M : A short textual representation of a month, three letters
y : A two digit representation of a year
...etc
Yii does not use the same format :
http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
You should simply use CDateFormatter::format() : http://www.yiiframework.com/doc/api/1.1/CDateFormatter#format-detail