Wednesday 24 July 2019

The Date Problem for Laravel and Regional Settings

For every Laravel application birthday should be displayed as it was saved. It should have a type 'date' and those 'created_at' should be 'datetime'
From New Zealand the record could be created Friday but for Cooks Island the record can be Thursday entry.
This is where PHP Carbon date comes to uses and Javascript Moments.

Install Moment and format as required. In VueJS create a filter


import moment from 'moment';

import moment from 'moment';
import {VueConstructor} from 'vue';

export const filters = {
  period: (value: string): string => {
    if (!value) return '-';
    return moment(value).format('MMM YYYY');
  },
  periodFullMonth: (value: string): string => {
    if (!value) return '-';
    return moment(value).format('MMMM YYYY');
  },
  date: (value: string): string => {
    if (!value) return '-';
    return moment.utc(value).local().format('DD-MM-YYYY');
  },
  absoluteDate: (value: string): string => {
    if (!value) return null;
    return moment(value).format('DD-MM-YYYY');
  },
};

export const createFilters = (vue: VueConstructor) => {
  vue.filter('period', filters.period);
  vue.filter('date', filters.date);
  vue.filter('absoluteDate', filters.absoluteDate);
  vue.filter('periodFullMonth', filters.periodFullMonth);
};

export default filters;

You might need to format the fields requests when saving


        Model::create([
            'began_at' => Carbon::createFromFormat('d-m-Y', $request->get('began_at')),
            'terminated_at' => $request->get('terminated_at'),
        ]);
Database Migration Scripts


         $table->dateTime('created_at');  
         $table->date('birthdate');  
Share:

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. I was nervous when I Clicked on this on this blog I felt what im looking for this blogs haves but thank you to the author who has everything in this blog for what im looking for. This blog writer has the best knowledge about laravel.

    If you're looking for Laravel Web Development Services in India so, you can visit our CodeKing Solutions for the best offers.

    ReplyDelete

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.