bludit/bl-kernel/helpers/date.class.php

131 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-03-08 18:02:59 +01:00
class Date {
2017-12-15 23:58:29 +01:00
// Returns string with the date translated
// Example: $date = 'Mon, 27th March' > 'Lun, 27th Marzo'
public static function translate($date)
{
global $L;
2017-12-15 23:58:29 +01:00
// If English default language don't translate
if ($L->currentLanguage()=='en') {
2017-12-15 23:58:29 +01:00
return $date;
}
2017-12-31 17:35:05 +01:00
// Get the array of dates from the language file
$dates = $L->getDates();
2017-12-31 17:35:05 +01:00
foreach ($dates as $english=>$anotherLang) {
$date = preg_replace('/\b'.$english.'\b/u', $anotherLang, $date);
}
return $date;
2017-12-15 23:58:29 +01:00
}
2015-03-08 18:02:59 +01:00
// Return current Unix timestamp, GMT+0
2015-05-05 03:00:01 +02:00
public static function unixTime()
2015-03-08 18:02:59 +01:00
{
return time();
}
2015-08-26 05:42:32 +02:00
// Return the local time/date according to locale settings.
public static function current($format)
2015-03-08 18:02:59 +01:00
{
2015-08-26 05:42:32 +02:00
$Date = new DateTime();
$output = $Date->format($format);
2017-12-15 23:58:29 +01:00
return self::translate($output);
2015-08-26 05:42:32 +02:00
}
2018-06-03 21:51:47 +02:00
// Returns the current time shifted by offset
// $offest could be +1 day, +1 month
2015-10-20 05:14:28 +02:00
public static function currentOffset($format, $offset)
{
$Date = new DateTime();
$Date->modify($offset);
$output = $Date->format($format);
return self::translate($output);
}
public static function offset($date, $format, $offset)
{
$Date = new DateTime($date);
$Date->modify($offset);
$output = $Date->format($format);
2017-12-15 23:58:29 +01:00
return self::translate($output);
2015-10-20 05:14:28 +02:00
}
2015-08-26 05:42:32 +02:00
// Format a local time/date according to locale settings.
public static function format($date, $currentFormat, $outputFormat)
{
2015-12-01 00:07:06 +01:00
// Returns a new DateTime instance or FALSE on failure.
2015-08-26 05:42:32 +02:00
$Date = DateTime::createFromFormat($currentFormat, $date);
2015-03-08 18:02:59 +01:00
2017-11-16 23:22:55 +01:00
if ($Date!==false) {
$output = $Date->format($outputFormat);
2017-12-15 23:58:29 +01:00
return self::translate($output);
2015-12-01 00:07:06 +01:00
}
return false;
2015-03-08 18:02:59 +01:00
}
public static function convertToUTC($date, $currentFormat, $outputFormat)
{
$Date = DateTime::createFromFormat($currentFormat, $date);
$Date->setTimezone(new DateTimeZone('UTC'));
$output = $Date->format($outputFormat);
2017-12-15 23:58:29 +01:00
return self::translate($output);
}
2015-03-08 18:02:59 +01:00
public static function timeago($time)
{
$time = time() - $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
2015-05-15 00:07:45 +02:00
// DEBUG: Check this function, need to be more fast
2015-03-08 18:02:59 +01:00
// Return array('Africa/Abidjan'=>'Africa/Abidjan (GMT+0)', ..., 'Pacific/Wallis'=>'Pacific/Wallis (GMT+12)');
// PHP supported list. http://php.net/manual/en/timezones.php
2015-05-15 00:07:45 +02:00
public static function timezoneList()
2015-03-08 18:02:59 +01:00
{
$tmp = array();
$timezone_identifiers_list = timezone_identifiers_list();
foreach($timezone_identifiers_list as $timezone_identifier)
{
$date_time_zone = new DateTimeZone($timezone_identifier);
$date_time = new DateTime('now', $date_time_zone);
$hours = floor($date_time_zone->getOffset($date_time) / 3600);
$mins = floor(($date_time_zone->getOffset($date_time) - ($hours*3600)) / 60);
$hours = 'GMT' . ($hours < 0 ? $hours : '+'.$hours);
$mins = ($mins > 0 ? $mins : '0'.$mins);
$text = str_replace("_"," ",$timezone_identifier);
2015-10-19 00:45:58 +02:00
$tmp[$timezone_identifier] = $text.' ('.$hours.':'.$mins.')';
2015-03-08 18:02:59 +01:00
}
2015-05-15 00:07:45 +02:00
return $tmp;
2015-03-08 18:02:59 +01:00
}
}