Carbon - A textual month could not be found Trailing data - laravel-4

I'm using Laravel. I get the following error when trying to display a date:
A textual month could not be found Trailing data
Format is j M Y, date is 2014-12-13 10:00:00
I don't get where is the error.
Any toughts?

For future reference: Using createFromFormat with the second parameter not being a Carbon object was giving me this same error. Using Carbon::parse:: instead of createFromFormat seemed to fix the issue:
public function setPublishedAtAttribute($date) {
$this->attributes['published_at'] = Carbon::parse($date);
}

Related

Laravel - different formats for date/time query

I am learning Laravel and I have some small problem on controllers - when I use DB, the query returns date time without timezone but if I use model, the query returns full datetime.
public function test($switch)
{
//return "YYYY-MM-DDThh:mm:ss.000000Z"
if ($switch) return Position::select('id','created_at')->orderBy('id')->get();
// return "YYYY-MM-DD hh:mm:ss"
return DB::table('positions')->select('id','created_at')->orderBy('id')->get();
}
Why? What I need to dof I want "YYYY-MM-DDThh:mm:ss.000000Z" on both cases?
Thanks for solution.
thanks for advice
You should use the DB::raw
The following statement will convert the datetime value created_at from +00:00 timezone to +10:00 timezone.
You can try this
return DB::table('positions')->select(DB::raw('id',CONVERT_TZ('created_at','+00:00','+10:00'))->orderBy('id')->get();
you can set your timezone that you wants to convert it
They are the same data, probably just different classes of date and you can always format your date. Laravel utilizes Carbon date library which is excellent and should be used primarily.
If you try to print out your date class with get_class() for Eloquent Position created_at, you probably got Carbon and DB::table('positions') created_at, you probably got DateTime and that's why the value looks different (but you still got the same date).
If you want to convert your DateTime to Carbon, you can do
$newDate = new \Carbon\Carbon($position->created_at)
Thanks Anurat,
I realized this fact shortly after sending the previous question.
... but there is another 'issue' - both times are my local time - time in "YYYY-MM-DDThh:mm:ss.000000Z" is not UTC time as I expected.
I changed my function:
public function test($switch = false)
{
$data = Position::selectRaw('id, created_at, UNIX_TIMESTAMP(created_at) unix')->orderBy('id')->get();
foreach ($data as $d) {
$conv = new \DateTime($d->created_at);
$d->conv = intval($conv->format('U'));
$d->diff = $d->conv - $d->unix;
}
return $data;
}
... and result is
0
id 1
created_at "2021-03-18T12:36:59.000000Z"
unix 1616067419
conv 1616071019
diff 3600
As you see, the difference is 1 hour (as my timezone offset). Where is a problem?
Thanks.

Laravel Datepicker Failed to parse time string

I'm trying to build a query in Laravel using whereBetween and I have a problem with the dates-range. I'm using Carbon to get the inputs, looking like this:
$dateRange = Carbon::parse($request->get('anniversary'));
I received the following error on submit:
DateTime::__construct(): Failed to parse time string (06/01/2019 - 06/30/2019) at position 11 (-): Unexpected character
Then, I changed the $dateRange in this form:
$dateRange = Carbon::parse(str_replace('-', '', $request->get('anniversary')));
After that, this error occured:
DateTime::__construct(): Failed to parse time string (06/01/2019 06/30/2019) at position 12 (0): Double date specification
The whereBetween clause looks like this:
->whereBetween('anniversary', [$dateRange])
Any ideas on how can I fix this?
You need to explode the retrieved Datepicker to two values. (Start Date and End Date)
$dateArray = explode('-', $request->get('anniversary'));
$startDate = $dateArray[0];
$endDate = $dateArray[1];
Now you can use
->whereBetween('anniversary', $dateArray);

Error when using the format: $table->date('column_name') in Laravel

I'm having some trouble with date formats in Laravel.
I have a database migration which has a field that looks like the following:
$table->date('Date');
Then, in the controller, I try to fill this field in with the following content:
2019-04-15
But I get the following error:
invalid datetime format: 1292 Incorrect date value: '"2019-04-15"' for column 'Date' at row 1
Any help on which would be the correct way of declaring the field 'Date' in the database would be apreciated.
Thank you very much in advance.
It's to do with strict date typing in MySQL I believe.
Check these answers for more information.
https://stackoverflow.com/a/47946950/1848660
https://stackoverflow.com/a/48998959/1848660
Try using Carbon to generate dates and timestamps.
This will generate a timestamp for the current date and time.
Carbon\Carbon::now();
Or you can parse your current date and convert it to a Carbon stamp
Carbon\Carbon::parse($date)->toDateTimeString();
Documentation:
https://carbon.nesbot.com/docs/
This works with your case:
$date = "2019-04-15";
$formatted_date = Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d');
You can go ahead and test your insert. I have tested this. You won't face the 1292 Incorrect date ERROR.
DB::table('yourTable')->insert(['date'=> $formatted_date]);
Try with this,
$table->date ? date('Y-m-d', strtotime($table->date)) : '-';
You should convert your date to string and then apply date function on that for different date format.

Converting String Values into Date using Carbon Laravel

The sql field type is VARCHAR, I am saving date and time from an API. The format that I get is 2018-04-28T22:17:41+05:30. Now I need to convert this using carbon to get a format like 04 April 2018 and timings. I couldn't convert varchar filed value into date format which I needed.
And the expected format should be passed to view, I did that using ( $ticket->dateOfIssue and it's giving this - 018-04-28T22:17:41+05:30 ). But as I said, I need the expected format (which is 04 April 2018, time ).
You should either use Accessor on your model or set the datetime format on your model. The example of the former is:
use Carbon/Carbon; before your class declaration
public function getDateOfIssueAttribute($value) {
return Carbon::parse($value)->format('d M Y , H:m:s');
}
When ever you retrieve this model anywhere you already have it in the format you set in your accessor.
Here is an example to parse the date
Carbon::parse('2018-04-28T22:17:41+05:30')->format('dd MM YYYY');
Moreover, do not forget to import the Carbon namespaces on the top
You try
use use Carbon\Carbon; // on top
$time = Carbon::parse('2018-04-28T22:17:41+05:30')->format('d M Y'); //28 Apr 2018
Good luck
This is the exact format for date and time. First of all include carbon library in your controller
use Carbon\Carbon;
class abc extend Controller
{
public function cancell()
{
$ticket = Booking::all()->where('status', '=', 'CANCELLED');
$dateOfIssue=$ticket->dateOfIssue;
$time = Carbon::parse($dateOfIssue)->format('d M Y , H:m:s');
return view('Admin.Tickets.cancelledTickets')->with('ticket', $ticket);
}
}
If you have multiple records then you can use loop for that
Please try ,
\Carbon\Carbon::parse('2018-04-28T22:17:41+05:30')->format('d- M- Y');
the output is 28- Apr- 2018
using sql
DATE_FORMAT(mydate, '%d-%M-%Y')

Convert unix timestamp to normal Date in Json

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/

Resources