Change the Date Format in Laravel View Page

change the date format in laravel view page

Try this:

date('d-m-Y', strtotime($user->from_date));

It will convert date into d-m-Y or whatever format you have given.

Note: This solution is a general solution that works for php and any of its frameworks. For a Laravel specific method, try the solution provided by Hamelraj.

How to change the date format in laravel view?

Laravel created_at is a Carbon instance, just check Carbon documentation to format the date as you want :

echo $created_at->format('j F Y');        
// 14 july 2017

or if you're getting just a string parse it and then format it :

Carbon\Carbon::parse($created_at)->format('j F Y')

i think it should work like this :

{{ \App\Calllog::orderBy('created_at','desc')->where('jobseeker‌​_id',$jobseeker->id)‌​->first()->created_a‌​t->format('j F Y') }} 

Carbon Formats

Laravel Create Date return different format

Add a serializeDate method to your model to change it's format on responses.

/**
* Prepare a date for array / JSON serialization.
*
* @param \DateTimeInterface $date
* @return string
*/
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}

Changing date format - Laravel

I'm actually going to just suggest that you stop storing date information in a text varchar column. Instead, you should be using a proper MySQL date column. Here is how you can rectify the situation from MySQL:

ALTER TABLE product ADD COLUMN new_expiry DATE;

UPDATE product
SET new_expiry = CASE WHEN expiry REGEXP '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
THEN CAST(expiry AS DATE)
ELSE STR_TO_DATE(expiry, '%m-%d-%Y') END;

ALTER TABLE product DROP COLUMN expiry;
ALTER TABLE product RENAME COLUMN new_expiry TO expiry;

I wouldn't even bother trying to manage this column from Laravel. Even if someone gives you an answer, it will be ugly and a lot of work, and also not best practice.

How to Change Date Format From Database In Controller Laravel 7

Laravel 7 uses a new date serialization format when using the toArray or toJson method on Eloquent models.

If you would like to keep using the previous behavior you can override the serializeDate() method on your model :

use DateTimeInterface;

protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d');
}

See the official upgrade doc here [7.x]

change the time format in laravel view page

I should probably add this as an answer so someone can find it later. Use str_pad to your advantage here.

You have minutes and hours, both of which could be a 1-digit or 2-digits number. Design your code to account for that. Let's see the code. You can adapt it to your needs.

<?php

function formattedTime ($hours, $minutes) {
return str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . "\n";
}

$hours = 7;
$minutes = 3;
echo formattedTime($hours, $minutes); // 07:03

$hours = 10;
$minutes = 3;
echo formattedTime($hours, $minutes); // 10:03

$hours = 7;
$minutes = 30;
echo formattedTime($hours, $minutes); // 07:30

$hours = 10;
$minutes = 30;
echo formattedTime($hours, $minutes); // 10:30

?>

Example: https://rextester.com/HOPJR15721

How to change date format in PHP Laravel?

Try this

    $date = '03/01/2021 - 03/01/2021';

$results = [];

preg_match_all('#\d{2}/\d{2}/\d{4}#', $date, $results);

$formattedDate = array_map(function ($date) {
return date('Y/m/d', strtotime($date));
}, $results[0]);

$date = "{$formattedDate[0]} - {$formattedDate[1]}";

result

"2021/03/01 - 2021/03/01"


Related Topics



Leave a reply



Submit