216 lines
5.1 KiB
PHP
216 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Core;
|
|
|
|
use App;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Str;
|
|
|
|
class DateTime
|
|
{
|
|
public static function getMonthName($date, $short = true)
|
|
{
|
|
return $short ? self::getUltraShortMonthName($date) : $date->monthName;
|
|
}
|
|
|
|
public static function getShortMonthName($date)
|
|
{
|
|
return $date->shortMonthName;
|
|
}
|
|
|
|
public static function getUltraShortMonthName($date)
|
|
{
|
|
return strtoupper(Str::ascii(mb_substr($date->shortMonthName, 0, 3)));
|
|
}
|
|
|
|
public static function getDayName($date, $short = true)
|
|
{
|
|
return $short ? $date->shortDayName : $date->dayName;
|
|
}
|
|
|
|
public static function getDateTime()
|
|
{
|
|
return self::datetimeToLocale(date('Y-m-d H:i:s'));
|
|
}
|
|
|
|
public static function getDate()
|
|
{
|
|
return self::dateToLocale(date('Y-m-d'));
|
|
}
|
|
|
|
public static function getLang()
|
|
{
|
|
return App::currentLocale();
|
|
// return session('locale') ? session('locale') : 'fr';
|
|
}
|
|
|
|
public static function datetoLocale($date = null)
|
|
{
|
|
$format = self::getLocaleFormatDate();
|
|
if ($date) {
|
|
$date = Carbon::parse($date)->format($format);
|
|
} else {
|
|
$date = today()->format($format);
|
|
}
|
|
|
|
return $date;
|
|
}
|
|
|
|
public static function datetimeToLocale($date = null)
|
|
{
|
|
$format = self::getLocaleFormatDatetime();
|
|
if ($date) {
|
|
$date = Carbon::parse($date)->format($format);
|
|
} elseif ($date === 'now') {
|
|
$date = now()->format($format);
|
|
}
|
|
|
|
return $date;
|
|
}
|
|
|
|
public static function countDaysFrom($date)
|
|
{
|
|
$begin = DateTime::getCarbonTime($date);
|
|
|
|
return $begin->diffInDays(now()) + 1;
|
|
}
|
|
|
|
public static function getCarbonTime($date)
|
|
{
|
|
$format = self::getLocaleFormatDatetime();
|
|
if (strlen($date) === 16) {
|
|
$date .= ':00';
|
|
}
|
|
|
|
return $date ? Carbon::createFromFormat($format, $date) : null;
|
|
}
|
|
|
|
public static function getCarbonDate($date)
|
|
{
|
|
$format = self::getLocaleFormatDate();
|
|
|
|
return $date ? Carbon::createFromFormat($format, $date) : null;
|
|
}
|
|
|
|
public static function convert($date)
|
|
{
|
|
return $date ? self::getCarbonDate($date)->isoFormat('Y-MM-DD') : null;
|
|
}
|
|
|
|
public static function convertTime($date)
|
|
{
|
|
return $date ? self::getCarbonTime($date)->isoFormat('Y-MM-DD HH:mm:ss') : null;
|
|
}
|
|
|
|
public static function toLocale($date)
|
|
{
|
|
$format = self::getLocaleFormatDate();
|
|
|
|
return $date ? Carbon::parse($date)->Format($format) : null;
|
|
}
|
|
|
|
public static function toISO($date)
|
|
{
|
|
return $date ? Carbon::parse($date)->isoFormat('Y-MM-DD') : null;
|
|
}
|
|
|
|
public static function toFr($date)
|
|
{
|
|
return $date ? Carbon::parse($date)->isoFormat('DD/MM/Y') : null;
|
|
}
|
|
|
|
public static function toFrTime($date)
|
|
{
|
|
return $date ? Carbon::parse($date)->isoFormat('DD/MM/Y HH:mm:ss') : null;
|
|
}
|
|
|
|
public static function getYearFromDate($date)
|
|
{
|
|
$date = self::convert($date);
|
|
$date = date_create($date);
|
|
|
|
return date_format($date, 'Y');
|
|
}
|
|
|
|
public static function getLocaleFormatDate()
|
|
{
|
|
$locale = self::getLang();
|
|
switch ($locale) {
|
|
case 'fr':
|
|
case 'en':
|
|
$format = 'd/m/Y';
|
|
break;
|
|
default:
|
|
$format = 'Y-m-d';
|
|
}
|
|
|
|
return $format;
|
|
}
|
|
|
|
public static function checkDateFormat($date)
|
|
{
|
|
if (preg_match("/^\d{2}\/\d{2}\/\d{4}$/", $date)) {
|
|
$day = substr($date, 0, 2);
|
|
$month = substr($date, 3, 2);
|
|
$year = substr($date, 6, 4);
|
|
if (checkdate($month, $day, $year)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getLocaleFormatDatetime()
|
|
{
|
|
$locale = self::getLang();
|
|
switch ($locale) {
|
|
case 'fr':
|
|
case 'en':
|
|
$format = 'd/m/Y H:i:s';
|
|
break;
|
|
default:
|
|
$format = 'Y-m-d H:i:s';
|
|
}
|
|
|
|
return $format;
|
|
}
|
|
|
|
public static function getLocaleDateFull($date = false)
|
|
{
|
|
return self::getISOFormat('LL', $date);
|
|
}
|
|
|
|
public static function getLocaleDateTimeFull($date = false)
|
|
{
|
|
return self::getISOFormat('LLL', $date);
|
|
}
|
|
|
|
public static function getLocaleDateFullShort($date = false)
|
|
{
|
|
return self::getISOFormat('ll', $date);
|
|
}
|
|
|
|
public static function getLocaleDateTimeFullShort($date = false)
|
|
{
|
|
return self::getISOFormat('lll', $date);
|
|
}
|
|
|
|
public static function getLocaleTime($date = false)
|
|
{
|
|
return self::getISOFormat('LT', $date);
|
|
}
|
|
|
|
public static function getISOFormat($format, $date = false)
|
|
{
|
|
$date = $date ? $date : now();
|
|
|
|
return Carbon::parse($date)->isoFormat($format);
|
|
}
|
|
|
|
public static function convertTimeToDate($date)
|
|
{
|
|
return substr($date, 0, 10);
|
|
}
|
|
}
|