Description
This function is an alias of: DateTime::sub()
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Коментарии
You cannot replace date_sub('2000-01-20') by DateTime::sub('2000-01-20') because DateTime::sub is not static. You have to create the DateTime object first.
Example:
<?php $dateA = date_sub('2000-01-20', date_interval_create_from_date_string('10 days')); ?>
will be replace by
<?php
$dateB = new DateTime('2000-01-20');
$dateA = $dateB->sub(date_interval_create_from_date_string('10 days'));
?>
To glue to the OOP, it's better to use it with DateInterval::createFromDateString
<?php
$dateB = new DateTime('2020-12-20');
$dateA = $dateB->sub(DateInterval::createFromDateString('10 days'));
?>
more detail here :
<?php
public static function createFromDateString ($time) {}
?>
It can be unclear for someone how to use this function.
Here is the example:
$date=date_create("2013-03-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
In version 5.6.31 the variable $ today is passed by reference in the function date_sub () and the interval is also applied
<?php
$today = date_create(date('Y-m-d'));
$yesterday = date_sub($today, date_interval_create_from_date_string("1 days"));
echo var_dump($today);
echo var_dump($yesterday)
?>