196 lines
3.6 KiB
PHP
196 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: Marco Glietsch
|
|
* Date: 14.05.2018
|
|
* Time: 16:20
|
|
*/
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\Publication;
|
|
use \Carbon\Carbon;
|
|
|
|
class DateHelper
|
|
{
|
|
static function toCal($timestamp, $addHours = 0)
|
|
{
|
|
$timestamp = strtotime($timestamp);
|
|
// Sommerzeit berücksichtigen
|
|
if(date("I", $timestamp))
|
|
{
|
|
$timestamp = $timestamp - 1 * 3600;
|
|
}
|
|
|
|
// Zeittone berücksichtigen GMT +1
|
|
$timestamp = $timestamp - 1 * 3600;
|
|
|
|
$timestamp = Carbon::createFromTimestamp($timestamp);
|
|
if($addHours > 0)
|
|
{
|
|
$timestamp->addHours($addHours);
|
|
}
|
|
|
|
return $timestamp->format('Ymd\THis\Z');
|
|
}
|
|
|
|
static function toHumanDate($timestamp)
|
|
{
|
|
$timestamp = Carbon::createFromFormat(config('formats.datetime.database'), $timestamp);
|
|
|
|
return $timestamp->format('d.m.Y');
|
|
}
|
|
|
|
static function toHumanTime($timestamp)
|
|
{
|
|
$timestamp = Carbon::createFromFormat(config('formats.datetime.database'), $timestamp);
|
|
|
|
return $timestamp->format('H:i');
|
|
}
|
|
|
|
static function toHumanDateTime($timestamp)
|
|
{
|
|
$timestamp = Carbon::createFromFormat(config('formats.datetime.database'), $timestamp);
|
|
|
|
return $timestamp->format('d.m.Y H:i:s');
|
|
}
|
|
|
|
static function daysToPublicationString($date)
|
|
{
|
|
$days = DateHelper::diffInDaysToNow($date);
|
|
|
|
if($days < 0)
|
|
{
|
|
return __("general.immediately");
|
|
}
|
|
else if($days == 0)
|
|
{
|
|
return __("general.today");
|
|
}
|
|
else if($days == 1)
|
|
{
|
|
return __("general.tomorrow");
|
|
}
|
|
else
|
|
{
|
|
return __("general.in :days days on :date", [
|
|
'days' => $days,
|
|
'date' => DateHelper::toHumanDate($date)]
|
|
);
|
|
}
|
|
}
|
|
|
|
static function daysSincePublicationString($date)
|
|
{
|
|
$days = DateHelper::diffInDaysToNow($date);
|
|
|
|
if($days < -1)
|
|
{ $days *= -1;
|
|
return "vor $days Tagen, am ".DateHelper::toHumanDate($date);
|
|
}
|
|
else if($days == -1)
|
|
{
|
|
return __("general.yesterday");
|
|
}
|
|
else if($days == 0)
|
|
{
|
|
return __("general.today");
|
|
}
|
|
else
|
|
{
|
|
return ___("admin.not published yet");
|
|
}
|
|
}
|
|
|
|
static function diffInDaysString($date)
|
|
{
|
|
if(empty($date))
|
|
{
|
|
return "nie";
|
|
}
|
|
$days = DateHelper::diffInDaysToNow($date);
|
|
|
|
if($days < -1)
|
|
{
|
|
return "vor ".(-1 * $days)." Tagen, am ".DateHelper::toHumanDate($date);
|
|
}
|
|
else if($days == -1)
|
|
{
|
|
return __("general.yesterday");
|
|
}
|
|
else if($days == 0)
|
|
{
|
|
return __("general.today");
|
|
}
|
|
else if($days == 1)
|
|
{
|
|
return __("general.tomorrow");
|
|
}
|
|
else
|
|
{
|
|
return __("general.in :days days on :date", [
|
|
'days' => $days,
|
|
'date' => DateHelper::toHumanDate($date)]
|
|
);
|
|
}
|
|
}
|
|
|
|
static function diffInDaysAndTimeString($date)
|
|
{
|
|
if(empty($date))
|
|
{
|
|
return "nie";
|
|
}
|
|
$days = DateHelper::diffInDaysToNow($date);
|
|
|
|
$daystring = "";
|
|
$timestring = __("general.at :time", [
|
|
'time' => DateHelper::toHumanTime($date)
|
|
]);
|
|
|
|
if($days < -1)
|
|
{
|
|
$daystring = __("general.:days days ago on :date", [
|
|
'days' => (-1 * $days),
|
|
'date' => DateHelper::toHumanDate($date)
|
|
]);
|
|
}
|
|
else if($days == -1)
|
|
{
|
|
$daystring = __("general.yesterday");
|
|
}
|
|
else if($days == 0)
|
|
{
|
|
$daystring = __("general.today");
|
|
}
|
|
else if($days == 1)
|
|
{
|
|
$daystring = __("general.tomorrow");
|
|
}
|
|
else
|
|
{
|
|
$daystring = __("general.in :days days on :date", [
|
|
'days' => $days,
|
|
'date' => DateHelper::toHumanDate($date)]
|
|
);
|
|
}
|
|
|
|
return $daystring." ".$timestring;
|
|
}
|
|
|
|
static public function diffInDaysToNow($date)
|
|
{
|
|
$now = Carbon::now()->endOfDay();
|
|
$days = $now->diffInDays($date);
|
|
if($now > $date)
|
|
{
|
|
$days *= -1;
|
|
}
|
|
else
|
|
{
|
|
$days++;
|
|
}
|
|
|
|
return $days;
|
|
}
|
|
} |