2019-04-22 13:59:04 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2019-04-22 13:59:04 +02:00
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2019-10-03 14:17:48 +02:00
|
|
|
use Carbon\Carbon;
|
2019-08-28 02:58:13 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
2019-04-22 13:59:04 +02:00
|
|
|
/**
|
2019-04-23 00:41:32 +02:00
|
|
|
* Class MakesDates
|
2019-04-22 13:59:04 +02:00
|
|
|
* @package App\Utils\Traits
|
|
|
|
*/
|
2019-04-23 00:41:32 +02:00
|
|
|
trait MakesDates
|
2019-04-22 13:59:04 +02:00
|
|
|
{
|
|
|
|
|
2019-08-28 01:19:54 +02:00
|
|
|
/**
|
|
|
|
* Converts from UTC to client timezone
|
|
|
|
* @param datetime object $utc_date
|
|
|
|
* @param string $timezone ie Australia/Sydney
|
|
|
|
* @return Carbon Carbon object
|
|
|
|
*/
|
2019-04-22 13:59:04 +02:00
|
|
|
public function createClientDate($utc_date , $timezone)
|
|
|
|
{
|
2019-08-28 04:36:53 +02:00
|
|
|
|
2019-08-28 02:58:13 +02:00
|
|
|
if(is_string($utc_date))
|
|
|
|
$utc_date = $this->convertToDateObject($utc_date);
|
2019-10-08 01:05:41 +02:00
|
|
|
|
2019-04-23 13:44:41 +02:00
|
|
|
return $utc_date->setTimezone(new \DateTimeZone($timezone));
|
2019-04-22 13:59:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:58:13 +02:00
|
|
|
/**
|
|
|
|
* Converts from client timezone to UTC
|
|
|
|
* @param datetime object $utc_date
|
|
|
|
* @param string $timezone ie Australia/Sydney
|
|
|
|
* @return Carbon Carbon object
|
|
|
|
*/
|
2019-04-22 14:17:52 +02:00
|
|
|
public function createUtcDate($client_date)
|
2019-04-22 13:59:04 +02:00
|
|
|
{
|
|
|
|
|
2019-08-28 02:58:13 +02:00
|
|
|
if(is_string($client_date))
|
|
|
|
$client_date = $this->convertToDateObject($client_date);
|
|
|
|
|
2019-04-23 13:44:41 +02:00
|
|
|
return $client_date->setTimezone(new \DateTimeZone('GMT'));
|
2019-04-22 13:59:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-29 00:13:26 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2019-10-03 14:17:48 +02:00
|
|
|
|
2019-08-29 00:13:26 +02:00
|
|
|
if(is_string($date))
|
|
|
|
$date = $this->convertToDateObject($date);
|
|
|
|
|
|
|
|
return $date->format($format);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:17:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
|
2019-10-08 01:05:41 +02:00
|
|
|
return Carbon::createFromTimestamp($timestamp)->format($format);
|
2019-10-03 14:17:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-28 02:58:13 +02:00
|
|
|
private function convertToDateObject($date)
|
|
|
|
{
|
|
|
|
|
|
|
|
return new \DateTime($date);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-29 00:13:26 +02:00
|
|
|
|
|
|
|
|
2019-04-22 13:59:04 +02:00
|
|
|
}
|