Files
xstore/inc/Hura8/System/TimeManager.php
2025-10-04 11:46:59 +07:00

154 lines
4.1 KiB
PHP

<?php
namespace Hura8\System;
class TimeManager
{
//convert date from javascript to database
public static function convert_date_from_javascript($date) {
//$date : dd-mm-yyyy or dd/mm/yyyy to yyyy-mm-dd
if (!$date) return "0000-00-00";
$date = str_replace('/', '-', $date);
if(!static::is_date($date)) return "0000-00-00";
list($day, $month, $year) = array_filter(explode("-", $date));
return join("-", [$year, $month, $day]); // $array[2] . "-" . $array[1] . "-" . $array[0];
}
// check format dd-mm-yyyy as date
public static function is_date($date){
return (preg_match('/[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}/', $date));
}
public static function format_date($time) {
if(!$time || $time == '0000-00-00') return '';
$today = date("d-m-Y");
$date_update = date("d-m-Y", strtotime($time));
if ($today == $date_update) {
return "Hôm nay";
}
return $date_update;
}
public static function format_time($time) {
$today = date("d-m-Y");
$date_update = date("d-m-Y", strtotime($time));
$hour_update = date("g:i a", strtotime($time));
if ($today == $date_update) {
return "Hôm nay, " . $hour_update;
}
return $date_update . ", " . $hour_update;
}
/**
* @param $from_date string dd-mm-YYYY
* @param $to_date string dd-mm-YYYY
* @return array [
[
"date" => 'd-m-Y',
"day" => 'Mon|Tue|Wed|Thu|Fri|Sat|Sun',
"date_in_year" => 'd-m',
],
// ...
]
*/
public static function get_date_range($from_date, $to_date = ''){
if(!$to_date) $to_date = $from_date;
$periods = new \DatePeriod(
new \DateTime(static::reverse_date($from_date)),
new \DateInterval('P1D'),
new \DateTime(static::reverse_date($to_date))
);
$range = [];
foreach ($periods as $key => $value) {
$range[] = [
"date" => $value->format('d-m-Y'),
"day" => $value->format('D'),
"date_in_year" => $value->format('d-m'),
];
}
$to_date_time = strtotime(static::reverse_date($to_date));
$range[] = [
"date" => $to_date,
"day" => date("D", $to_date_time),
"date_in_year" => date("d-m", $to_date_time),
];
return $range;
}
/**
* @param $from_date string dd-mm-YYYY
* @param $to_date string dd-mm-YYYY
* @return int number of days
*/
public static function get_day_diff($from_date, $to_date) {
$now = strtotime(static::reverse_date($to_date));
$your_date = strtotime(static::reverse_date($from_date));
$datediff = $now - $your_date;
return round($datediff / (60 * 60 * 24));
}
/**
* @param $date string dd-mm-YYYY
* @param $num_days int
* @return string dd-mm-YYYY
*/
public static function add_day($date, $num_days = 1) {
return date('d-m-Y', strtotime(static::reverse_date($date). ' + '.$num_days.' days'));
}
//Reverse date from yyyy-mm-dd to dd-mm-yyyy
public static function reverse_date($p_date) {
list($year, $month, $day) = explode("-", $p_date);
if(!$year || $year == '0000') return '';
return join("-", [$day, $month, $year]);
}
public static function getDateTimeForMySQL() {
return date("Y-m-d H:i:s");
}
/**
* @description convert date pattern: dd-mm-YYYY H:i to timestamp
* @param string $input_date_time dd-mm-YYYY H:i to timestamp
* @return int
*/
public static function convertDateTimeToInt($input_date_time) {
$check_date_pattern = "/\d{2}-\d{2}-\d{4}(\s+)?\d{1,2}:\d{1,2}/i";
$format_date_time = str_replace("/", "-", $input_date_time);
if(preg_match($check_date_pattern, $format_date_time)) {
list($date, $time) = array_values(array_filter(explode(" ", $format_date_time)));
return strtotime($date. " ". $time);
}
return 0;
}
}