2019-04-22 13:59:04 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-04-22 13:59:04 +02:00
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2019-10-03 14:17:48 +02:00
|
|
|
use Carbon\Carbon;
|
2020-10-28 11:10:49 +01:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
2019-08-28 02:58:13 +02:00
|
|
|
|
2019-04-22 13:59:04 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class MakesDates.
|
2019-04-22 13:59:04 +02:00
|
|
|
*/
|
2019-04-23 00:41:32 +02:00
|
|
|
trait MakesDates
|
2019-04-22 13:59:04 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Converts from UTC to client timezone.
|
2019-12-30 22:59:12 +01:00
|
|
|
* @param datetime object $utc_date
|
|
|
|
* @param string $timezone ie Australia/Sydney
|
|
|
|
* @return Carbon Carbon object
|
|
|
|
*/
|
|
|
|
public function createClientDate($utc_date, $timezone)
|
|
|
|
{
|
|
|
|
if (is_string($utc_date)) {
|
|
|
|
$utc_date = $this->convertToDateObject($utc_date);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
return $utc_date->setTimezone(new DateTimeZone($timezone));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Converts from client timezone to UTC.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param datetime object $utc_date
|
|
|
|
* @return Carbon Carbon object
|
2019-12-30 22:59:12 +01:00
|
|
|
*/
|
|
|
|
public function createUtcDate($client_date)
|
|
|
|
{
|
|
|
|
if (is_string($client_date)) {
|
|
|
|
$client_date = $this->convertToDateObject($client_date);
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
return $client_date->setTimezone(new DateTimeZone('GMT'));
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Formats a date.
|
2020-11-01 06:09:09 +01:00
|
|
|
* @param Carbon|string $date Carbon object or date string
|
2019-12-30 22:59:12 +01:00
|
|
|
* @param string $format The date display format
|
|
|
|
* @return string The formatted date
|
|
|
|
*/
|
|
|
|
public function formatDate($date, string $format) :string
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! isset($date)) {
|
2020-02-13 12:27:42 +01:00
|
|
|
return '';
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (is_string($date)) {
|
|
|
|
$date = $this->convertToDateObject($date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->format($format);
|
|
|
|
}
|
|
|
|
|
2021-01-08 03:15:55 +01:00
|
|
|
/**
|
|
|
|
* Formats a datedate.
|
2022-07-18 02:04:17 +02:00
|
|
|
* @param $date Carbon object or date string
|
2021-01-08 03:15:55 +01:00
|
|
|
* @param string $format The date display format
|
|
|
|
* @return string The formatted date
|
|
|
|
*/
|
|
|
|
public function formatDatetime($date, string $format) :string
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
return Carbon::createFromTimestamp($date)->format($format.' g:i a');
|
2021-01-08 03:15:55 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Formats a date.
|
2019-12-30 22:59:12 +01:00
|
|
|
* @param Carbon/String $date Carbon object or date string
|
|
|
|
* @param string $format The date display format
|
|
|
|
* @return string The formatted date
|
|
|
|
*/
|
|
|
|
public function formatDateTimestamp($timestamp, string $format) :string
|
|
|
|
{
|
|
|
|
return Carbon::createFromTimestamp($timestamp)->format($format);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convertToDateObject($date)
|
|
|
|
{
|
2021-01-12 08:40:18 +01:00
|
|
|
$dt = new DateTime($date);
|
2021-01-12 08:38:16 +01:00
|
|
|
$dt->setTimezone(new DateTimeZone('UTC'));
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-01-12 08:38:16 +01:00
|
|
|
return $dt;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2021-02-23 11:04:39 +01:00
|
|
|
|
|
|
|
public function translateDate($date, $format, $locale)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (empty($date)) {
|
2022-01-12 07:55:12 +01:00
|
|
|
return '';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-01-12 07:55:12 +01:00
|
|
|
|
2021-02-23 11:04:39 +01:00
|
|
|
Carbon::setLocale($locale);
|
|
|
|
|
2021-03-10 16:56:17 +01:00
|
|
|
try {
|
|
|
|
return Carbon::parse($date)->translatedFormat($format);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2021-03-10 16:56:17 +01:00
|
|
|
return 'Invalid date!';
|
|
|
|
}
|
2021-02-23 11:04:39 +01:00
|
|
|
}
|
2021-03-10 16:56:17 +01:00
|
|
|
}
|