1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-19 17:42:44 +01:00
invoiceninja/app/Utils/Traits/MakesDates.php

90 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
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
/**
* Class MakesDates.
*/
2019-04-23 00:41:32 +02:00
trait MakesDates
{
/**
* Converts from UTC to client timezone.
* @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-10-28 11:10:49 +01:00
return $utc_date->setTimezone(new DateTimeZone($timezone));
}
/**
* Converts from client timezone to UTC.
2020-10-28 11:10:49 +01:00
* @param datetime object $utc_date
* @return Carbon Carbon object
*/
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'));
}
/**
* Formats a date.
* @param Carbon|string $date Carbon object or date string
* @param string $format The date display format
* @return string The formatted date
*/
public function formatDate($date, string $format) :string
{
2020-11-25 15:19:52 +01:00
if (!isset($date)) {
return '';
2020-11-25 15:19:52 +01:00
}
// if (!$date || strlen($date) < 1) {
// return '';
// }
if (is_string($date)) {
$date = $this->convertToDateObject($date);
}
return $date->format($format);
}
/**
* Formats a date.
* @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)
{
2020-10-28 11:10:49 +01:00
return new DateTime($date);
}
}