I'm trying to convert date input to the proper mysql format.
My start_date field is formatted MM dd, yyyy.
In my controller store function I'm grabbing all the users input including start_date and saving.
$userProfileData->save();
What I thought I could do is us Carbon to convert the start_date before save like this.
$userProfileData->start_date->toDateString();
I get a Call to a member function toDateString() on a non-object error.
I was able to convert the date format by adding this to my users controller store function.
$userProfileData->start_date = date('Y-m-d', strtotime($userProfileData->start_date));
you get non-object error because start_date is not the object with method toDateString()
what you can do is use setStartDateAttribute() method in your model which will be invoked every time to get the start_date attribute value and in this method you can modify value so model will use the updated value for operation like edit or update it can be as follow
public function setStartDateAttribute($value) {
$this->attributes['start_date'] = date('Y-m-d', strtotime($value) );
}
the following method will used by your model when it needs to get the value of start_date attribute this method must be written in the model
Related
I have been trying to get the the Year and Month from the user and insert them into the database.
i am using input type month so that the user can send only the Year and the Month.
<input type="month" name="date_from"/>
<input type="month" name="date_to"/>
and this is my model
function setDateFromAttribute($value)
{
$this->attributes['date_from'] = \Carbon\Carbon::createDateFromFormat('Y-m', $value);
}
function setDateToAttribute($value)
{
$this->attributes['date_to'] = \Carbon\Carbon::createDateToFormat('Y-m', $value);
}
protected $fillable = [
'date_from',
'date_to',
];
and data is saved as 0000.00.00
the data type in my database for these two inputs is
timestamp
i do not know what i am doing wrong here. please help
I don't see createDateFromFormat as a valid Carbon method in the documentation https://carbon.nesbot.com/docs/
Likewise, there does not appear to be any createDateToFormat method.
Carbon::createFromFormat() returns a Carbon object, not a string or equivalent MySQL timestamp, so, assuming you change your code to be:
$this->attributes['date_from'] = \Carbon\Carbon::createFromFormat('Y-m', $value)->toDateTimeString();
It should provide the results you are looking for.
References:
Converting a carbon date to mysql timestamp.
#Ted Stresen-Reuter
Sorry for the late reply couldn't find a solution that works within the model. tried your method which i have tried before with minor changes.
thanks a lot for trying to help me.. i found a method to complete this inside the controller which i will not recommend, but this was the best i was able to find which works and i was on a tight schedule.
'date_from' => isset($date_from[$key]) ? date('Y-m-d h:i:s', strtotime($date_from[$key])) : '',
'date_to' => isset($date_to[$key]) ? date('Y-m-d h:i:s', strtotime($date_to[$key])) : '',
the type timestamp accepts at least dates in full formats. as i know how i pass the data from blade to the controller i was able to add a date method and within it a strttotime method to convert my YY-MM into YY-MM-DD and the date that is inserted by default will be 01.
I store in my database a date format like this:
2017-02-22 16:55:40
I added it to my database like this:
Carbon::now();
I need to check if 4 hours passed since this date.
How I can do this? I couldn't figure out how I can convert this format into Carbon or timestamp.
If you are using Laravel and the date is a Carbon instance from a Model you have access to the whole Carbon API.
You can use the Difference API of Carbon for this specific purpose.
echo $model->thedate->diffInHours($now, false);
If your model does not threat the date as a carbon instance you can cast it by adding the date to the dates array of the current model like so
protected $dates = [
'field_name',
];
Check out Date casting for more information
Update with an explicit example
$user = User::first();
// This will return the difference in hours
$user->created_at->diffInHours(Carbon\Carbon::now(), false);
You can convert it to a Carbon object with:
Carbon::parse('2017-02-22 16:55:40');
I want to create a completedDatetime that follows the datetime in the orderDatetime field.
Fixtures.yml
directive_{200..500}:
orderDatetime: <dateTimeThisYear()>
completedDatetime: '90%? <dateTimeBetween("orderDatetime", "now")>'
I used the code above in my fixtures file and got the data that follows.
Is there a way to ensure a sane result using faker data short of writing custom functions in LoadFixtures??
Since you are passing string "orderDatetime" to strtotime() function it returns 1970 year and your dateTimeBetween works like dateTimeBetween('1970', 'now'). Passing of variables is done with $orderDatetime. But if you pass such variable to dateTimeBetween(), the future dateTime might get passed which is not possible.
It only works if your order date is the past from now:
orderDatetime: '<dateTimeBetween("-200 days", "now")>'
completedDatetime: '90%? <dateTimeBetween($orderDatetime, "now")>'
If you wonna generate future dates then you need to create custom faker function for example:
public function dateTimeBetweenAfter($dateFrom, $dateTo)
{
$date = new DateTime($dateFrom->format('Y-m-d H:i:s'));
$dateTo = $date->modify($dateTo);
return FakerDateTime::dateTimeBetween($dateFrom, $dateTo);
}
and use it like this:
orderDatetime: <dateTimeThisYear()>
completedDatetime: '90%? <dateTimeBetweenAfter($orderDatetime, "+1 year")>'
How to get only date on database column, I have difficulties using Carbon on controller:
$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();
Date column looks like this in the database:
That is a DATETIME column, so there's no need for additional formatting the Carbon instance. However you need to use whereDate if you want the fetch all users for which the date column contains today's date:
$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();
Because when you pass Carbon::today() to the Query Builder method, the __toString method will be automatically called and return a DATETIME string with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.
I am using laravel 5.0. I am getting data from controller as json. I am having value like this.
{"timstamp_date":"1434360957"},
I need to convert this unix timestamp value as Normal Date Like (15-06-2015) or (15-March-2015).
I have used Date(timstamp_date) but it is showing current time only. Not my timstamp date
You could use:
date("d-m-Y H:i:s", 1434360957);
EDIT
You could try;
var dateTime = new Date(1434360957*1000);
var formatted = dateTime.toGMTString();
https://jsfiddle.net/sp57Lnpf/
Use the date function. You need to specify the format as the first parameter:
date("d-m-Y", $timestamp_date)
http://php.net/manual/en/function.date.php
Laravel also comes with Carbon you could use that if you wanted to for further manipulation of the data if you so required it.
http://carbon.nesbot.com/docs/