periodical/src/Dater.php

126 lines
3.2 KiB
PHP
Raw Normal View History

2023-03-24 22:12:11 +00:00
<?php
/**
* @brief periodical, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\periodical;
use DateTimeZone;
use dcCore;
use Exception;
/**
* Tools to manupilate period date
*/
class Dater
{
/**
* Format a date from UTC to user TZ.
*
* @param string $date The date
* @param string $format The output format
*
* @return string The formated date on user timezone
2023-03-24 22:12:11 +00:00
*/
public static function fromUser(string $date, string $format = 'Y-m-d H:i:00'): string
{
$tz = dcCore::app()->auth?->getInfo('user_tz');
$d = date_create($date, new DateTimeZone($tz ?? 'UTC'));
2023-03-24 22:12:11 +00:00
return $d ? date_format($d->setTimezone(new DateTimeZone('UTC')), $format) : '';
}
/**
* Format a date from user TZ to UTC.
*
* @param string $date The date
* @param string $format The output format
*
* @return string The formated date on UTC
2023-03-24 22:12:11 +00:00
*/
public static function toUser(string $date, string $format = 'Y-m-d\TH:i'): string
{
$tz = dcCore::app()->auth?->getInfo('user_tz');
$d = date_create($date, new DateTimeZone('UTC'));
2023-03-24 22:12:11 +00:00
return $d ? date_format($d->setTimezone(new DateTimeZone($tz ?? 'UTC')), $format) : '';
2023-03-24 22:12:11 +00:00
}
/**
* Format a date to specific TZ (UTC by default) from another format.
*
* @param string $date The date
* @param string $format The output format
* @param string $to_tz The output timezone
*
* @return string The formated date
2023-03-24 22:12:11 +00:00
*/
public static function toDate(int|string $date = 'now', string $format = 'Y-m-d H:i:00', string $to_tz = 'UTC'): string
{
$d = is_int($date) ?
date_create_from_format('U', (string) $date, new DateTimeZone('UTC')) :
date_create($date, new DateTimeZone('UTC'));
return $d ? date_format($d->setTimeZone(new DateTimeZone($to_tz)), $format) : '';
}
/**
* Get next timestamp from a period.
*
* @param int $ts The timestamp
* @param string $period The period (periodical string format)
*
* @return int The timestamp of next update
2023-03-24 22:12:11 +00:00
*/
public static function getNextTime(int $ts, string $period): int
{
$dt = date_create_from_format('U', (string) $ts);
if ($dt === false) {
return $ts;
}
switch($period) {
case 'hour':
$dt->modify('+1 hour');
break;
case 'halfday':
$dt->modify('+12 hours');
break;
case 'day':
$dt->modify('+1 day');
break;
case 'week':
$dt->modify('+1 week');
break;
case 'month':
$dt->modify('+1 month');
break;
default:
throw new Exception(__('Unknow frequence'));
}
return (int) $dt->format('U');
}
}