Skip to content

Instantly share code, notes, and snippets.

@necrophidia
Last active October 6, 2017 07:01
Show Gist options
  • Save necrophidia/dd5d102d89c9faca555602614c0910c5 to your computer and use it in GitHub Desktop.
Save necrophidia/dd5d102d89c9faca555602614c0910c5 to your computer and use it in GitHub Desktop.
Laravel Custom Date Helper
<?php // Date Helper
namespace App\Helpers;
use Carbon\Carbon;
class DateHelper
{
public static function parse_from_datepicker($date_raw)
{
$result = null;
$date_arr = explode('/', $date_raw);
if(count($date_arr) > 1){
$year = $date_arr[2];
$month = $date_arr[1];
$day = $date_arr[0];
$result = Carbon::create($year, $month, $day, 0, 0, 0);
}
return $result;
}
public static function datepicker($date)
{
if($date == null || $date == '00/00/0000') {
return '';
} else {
$parsed_date = Carbon::parse($date);
$day = $parsed_date->day;
if($day < 10) {
$day = '0' . $day;
}
$month = $parsed_date->month;
if($month < 10) {
$month = '0' . $month;
}
return $day . '/' . $month . '/' . $parsed_date->year;
}
}
public static function long_format($date)
{
if($date == null || $date == '00/00/0000') {
return '';
} else {
$parsed_date = Carbon::parse($date);
return $parsed_date->format('d F Y');
}
}
public static function time_format($date)
{
if($date == null || $date == '00/00/0000') {
return '';
} else {
$parsed_date = Carbon::parse($date);
return $parsed_date->format('d F Y H:i');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment