Skip to content

Instantly share code, notes, and snippets.

@anwas
Last active June 14, 2022 16:18
Show Gist options
  • Save anwas/b5c2e371074e9b1309713ce555474dec to your computer and use it in GitHub Desktop.
Save anwas/b5c2e371074e9b1309713ce555474dec to your computer and use it in GitHub Desktop.
[Date Time class and functions] #php #date #time #datetime #function #class
<?php
// Patern http://userguide.icu-project.org/formatparse/datetime
// Patern http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
$fmt = new IntlDateFormatter('lt_LT', IntlDateFormatter::FULL,
IntlDateFormatter::NONE, 'Europe/Vilnius', IntlDateFormatter::GREGORIAN, "cccc");
$timestamp = strtotime('now');
$timestamp = strtotime('today');
$timestamp = strtotime('+7 days');
$timestamp = strtotime('next day');
echo datefmt_format( $fmt , time() );
$todays_weekday = mb_strtolower( $fmt->format($timestamp) );
$fmt = datefmt_create( 'lt_LT', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Vilnius', IntlDateFormatter::GREGORIAN );
echo datefmt_format( $fmt , time() );
$fmt = datefmt_create( 'lt_LT', IntlDateFormatter::FULL, IntlDateFormatter::NONE, 'Europe/Vilnius', IntlDateFormatter::GREGORIAN, 'yyyy.MM.dd HH:mm' );
echo datefmt_format( $fmt , time() );
echo datefmt_format( $fmt , strtotime('next day') );
setlocale( LC_ALL, 'lt_LT.UTF-8', 'lt_LT', 'lt' );
$timestamp = strtotime('today');
echo strftime('%A', $timestamp);
$date = ( new DateTime( '+ 12 months' ) )->format( 'Y-m-d H:i:s' );
$date = ( new DateTime( 'last day of this months' ) )->format( 'Y-m-d H:i:s' );
$date = ( new DateTime( 'last day of this months + 6 months' ) )->format( 'Y-m-d H:i:s' );
$date = ( new DateTime( 'second Monday of October 2018' ) )->format( 'Y-m-d H:i:s' );
$date = ( new DateTime( '+ 1 day', new DateTimeZone( 'Europe/Vilnius' ) ) )->format( 'Y-m-d H:i:s' );
echo $date . '<br /><br />';
$date = ( new DateTime( 'now', new DateTimeZone( 'Europe/Vilnius' ) ) )->format( 'Y-m-d H:i:s' );
echo $date . '<br /><br />';
$date = ( new DateTime( '+ 3 minutes', new DateTimeZone( 'Europe/London' ) ) )->format( 'Y-m-d H:i:s' );
echo $date . '<br /><br />';
if ( ! function_exists( 'anwp_get_strtotime' ) ) {
function anwp_get_strtotime( $date = null, $format = null, $timezone = null, $lang = null ) {
// Patern http://userguide.icu-project.org/formatparse/datetime
// Patern http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
/**********************************************************************
* ***
* 'yyyy-MM-dd HH:mm:ss' – 2019-11-03 04:13:41
* 'GGGG' – po Kristaus
* 'cccc' – sekmadienis
* 'zzzz' – Rytų Europos žiemos laikas
* 'VV' – Europe/Vilnius
* 'OOOO' – GMT+02:00
* "yyyy 'm.' MMMM 'mėn.' d 'd.'" – 2019 m. lapkričio mėn. 3 d.
* 'LLLL' – lapkritis
*********************************************************************/
$date = ( is_null( $date ) ) ? 'now' : $date;
$format = ( is_null( $format ) ) ? 'yyyy-MM-dd HH:mm:ss, cccc – VV OOOO' : $format;
$timezone = ( is_null( $timezone ) ) ? 'Europe/Vilnius' : $timezone;
$lang = ( is_null( $lang ) ) ? 'lt_LT' : $lang;
$fmt = new IntlDateFormatter( "{$lang}", IntlDateFormatter::FULL, IntlDateFormatter::NONE, "{$timezone}", IntlDateFormatter::GREGORIAN, "{$format}" );
$datetime = new DateTime( "{$date}", new DateTimeZone( "{$timezone}" ) );
$fmt->setTimeZone( "{$timezone}" );
return $fmt->format( $datetime );
}
} // if ( ! function_exists( 'anwp_get_strtotime' ) )
if ( ! function_exists( 'anwp_get_x_days' ) ) {
function anwp_get_x_days( $args = null ) {
if ( is_null( $args ) || empty( $args ) || ! is_array( $args ) ) {
$args = [
'days' => 7,
'offset' => 0,
'direction' => 'last', // available: last || next
'format' => 'yyyy-MM-dd',
'timezone' => 'Europe/Vilnius',
'lang' => 'lt_LT',
];
}
$args['days'] = ( ! isset( $args['days'] ) ) ? 7 : $args['days'];
if ( $args['days'] <= 0 ) {
$args['days'] = 1;
}
$args['offset'] = ( ! isset( $args['offset'] ) || ! is_integer( $args['offset'] ) ) ? 0 : abs( $args['offset'] );
$args['direction'] = ( ! isset( $args['direction'] ) || ! is_string( $args['direction'] ) ) ? 'last' : mb_strtolower( $args['direction'], 'UTF-8' );
$args['format'] = ( ! isset( $args['format'] ) ) ? 'yyyy-MM-dd' : $args['format'];
$args['timezone'] = ( ! isset( $args['timezone'] ) ) ? 'Europe/Vilnius' : $args['timezone'];
$args['lang'] = ( ! isset( $args['lang'] ) ) ? 'lt_LT' : $args['lang'];
// Patern http://userguide.icu-project.org/formatparse/datetime
// Patern http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
/**********************************************************************
* ***
* 'yyyy-MM-dd HH:mm:ss' – 2019-11-03 04:13:41
* 'GGGG' – po Kristaus
* 'cccc' – sekmadienis
* 'zzzz' – Rytų Europos žiemos laikas
* 'VV' – Europe/Vilnius
* 'OOOO' – GMT+02:00
* "yyyy 'm.' MMMM 'mėn'. d 'd.'" – 2019 m. lapkričio mėn. 3 d.
* 'LLLL' – lapkritis
*********************************************************************/
$fmt = new IntlDateFormatter( "{$args['lang']}", IntlDateFormatter::FULL, IntlDateFormatter::NONE, "{$args['timezone']}", IntlDateFormatter::GREGORIAN, "{$args['format']}" );
$date_array = [];
$datetime = new DateTime( 'now', new DateTimeZone( "{$args['timezone']}" ) );
if ( $args['offset'] !== 0 && $args['direction'] === 'next' ) {
$datetime->modify( "+{$args['offset']} day" );
} else if ( $args['offset'] !== 0 && $args['direction'] === 'last' ) {
$datetime->modify( "-{$args['offset']} day" );
}
for ( $i = 0; $i < $args['days']; $i++ ) {
$date_array[] = $fmt->format( $datetime );
if ( $args['direction'] === 'last' ) {
$datetime->modify( '-1 day' );
} else {
$datetime->modify( '+1 day' );
}
}
// return array_reverse( $date_array );
return $date_array; // funkcijose tik vienas veiksmas, reikalui esant, gautą rezultatą bus galima apversti
}
} // if ( ! function_exists( 'anwp_get_x_days' ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment