View source
<?php
namespace Drupal\content_calendar;
use Drupal\Core\Datetime\DateHelper;
abstract class DateTimeHelper {
const FORMAT_MYSQL_DATE_ONLY_REGEX = '\\d{4}\\-\\d{2}\\-\\d{2}';
const FORMAT_MYSQL_DATE_ONLY = 'Y-m-d';
public static function getMonthLabelByNumber($number) {
if (is_numeric($number) && ($number >= 1 && $number <= 12)) {
$month_labels = [
1 => t('January'),
2 => t('February'),
3 => t('March'),
4 => t('April'),
5 => t('May'),
6 => t('June'),
7 => t('July'),
8 => t('August'),
9 => t('September'),
10 => t('October'),
11 => t('November'),
12 => t('December'),
];
return $month_labels[$number];
}
return FALSE;
}
public static function getDayCountInMonth($month, $year) {
return cal_days_in_month(CAL_GREGORIAN, $month, $year);
}
public static function getFirstDayOfMonth($month, $year) {
$datetime = new \DateTime();
$datetime
->setDate($year, $month, 1);
$datetime
->setTime(0, 0, 0);
return $datetime;
}
public static function getLastDayOfMonth($month, $year) {
$datetime = new \DateTime();
$datetime
->setDate($year, $month, 1);
$datetime
->setTime(23, 59, 59);
$datetime
->modify('last day of this month');
return $datetime;
}
public static function convertUnixTimestampToDatetime($unix_timestamp) {
$datetime = new \DateTime();
$datetime
->setTimestamp($unix_timestamp);
return $datetime;
}
public static function dateIsMySQLDateOnly($value) {
return preg_match("/" . self::FORMAT_MYSQL_DATE_ONLY_REGEX . "/", $value);
}
}