[php] Convert String to Carbon

I am using Laravel 5.1

Few days ago I used protected $dates = ['license_expire'] in my model to convert the string date to Carbon instances. In HTML the default value in create form for the date was Carbon\Carbon::now()->format('Y-m-d')

In order to show alert in home page i used <p>Licence Expired: <b>{{ $employee->license_expire < Carbon\Carbon::now()?'License has expired':$employee->license_expire->diffForHumans() }}</b></p>

Till then diffForHumans() method works fine.

But in that case the edit form's default value also was today's date no matter what was in database(I am using a partial form). To resolve it I change the default value in HTML was NUll. And add another method in my model to show current date in create form.

public function getLicenseExpireAttribute($date)
{
    return Carbon::parse($date)->format('Y-m-d');
}

After that when i go to home page i have an FatalErrorException which says Call to a member function diffForHumans() on string

When I check the date with dd($employee->license_expire) it become STRING again.

Can anybody tell me how can I convert the string to Carbon in this situation?

or

Make my create form's default date as today's date, the edit form's date from database and I can use diffForHumans() to show alert in home page?

This question is related to php date laravel-5.1 php-carbon

The answer is


Why not try using the following:

$dateTimeString = $aDateString." ".$aTimeString;
$dueDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $dateTimeString, 'Europe/London');   

Try this

$date = Carbon::parse(date_format($youttimestring,'d/m/Y H:i:s'));
echo $date;

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to laravel-5.1

How to clear Route Caching on server: Laravel 5.2.37 getting error while updating Composer Laravel 5 Application Key Get only records created today in laravel Laravel 5.1 API Enable Cors How to execute raw queries with Laravel 5.1? Convert String to Carbon laravel 5 : Class 'input' not found

Examples related to php-carbon

Laravel Carbon subtract days from current date Laravel 5 Carbon format datetime How to get Current Timestamp from Carbon in Laravel 5 Convert String to Carbon How to compare two Carbon Timestamps?