strtotime

(PHP 4, PHP 5)

strtotime — Преобразует текстовое представление даты на английском языке в метку времени Unix

Описание

int strtotime ( string $time [, int $now ] )

Первым параметром функции должна быть строка с датой на английском языке, которая будет преобразована в метку времени относительно метки времени, переданной в now , или текущего времени, если аргумент now опущен. В случае ошибки возвращается -1.

Функция strtotime() использует GNU формат даты, поэтому рекомендуется ознакомиться с руководством GNU » Date Input Formats, где описывается синтаксис аргумента time .

Пример #1 Пример использования функции strtotime()

<?php
echo strtotime("now"), "\n";
echo 
strtotime("10 September 2000"), "\n";
echo 
strtotime("+1 day"), "\n";
echo 
strtotime("+1 week"), "\n";
echo 
strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo 
strtotime("next Thursday"), "\n";
echo 
strtotime("last Monday"), "\n";
?>

Пример #2 Проверка ошибок

<?php
$str 
'Not Good';
if ((
$timestamp strtotime($str)) === -1) {
    echo 
"Строка ($str) недопустима";
} else {
    echo 
"$str == " date('l dS of F Y h:i:s A'$timestamp);
}
?>

Замечание: Для большинства систем допустимыми являются даты с 13 декабря 1901, 20:45:54 GMT по 19 января 2038, 03:14:07 GMT. (Эти даты соответствуют минимальному и максимальному значению 32-битового целого со знаком). Для Windows допустимы даты с 01-01-1970 по 19-01-2038. Не все платформы поддерживают отрицательные метки времени, поэтому даты ранее 1 января 1970 г. не поддерживаются в Windows, некоторых версиях Linux и некоторых других операционных системах.

Коментарии

Автор:
Be careful when using two numbers as the year. I came across this situation:

<?php

echo strtotime('24.11.22');
echo 
date('d.m.Y H:i:s'1669324282)  .  "\n\n";

// But
echo strtotime('24.11.2022');
echo 
date('d.m.Y H:i:s'1669237200);

?>

Output:

1669324282
25.11.2022 00:11:22

1669237200
24.11.2022 00:00:00
2023-03-17 17:39:01
http://php5.kiev.ua/manual/ru/function.strtotime.html
Be aware of this: 1 month before the 31st day, it will return the same month:

<?php
echo date('m'strtotime('2023-05-30 -1 month')) ; //returns 04
echo date('m'strtotime('2023-05-31 -1 month')) ; //returns 05, not 04
?>

So, don't use this to operate on the month of the result.
A better way to know what month was the previous month would be:

<?php
//considering today is 2023-05-31...

$firstOfThisMonth date('Y-m') . '-01'//returns 2023-05-01
echo date('m'strtotime($firstOfThisMonth ' -1 month')) ; //returns 04
?>
2023-06-01 01:02:57
http://php5.kiev.ua/manual/ru/function.strtotime.html
> The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.

important - does not contain

<?php
date_default_timezone_set
('Europe/Berlin');

// ....  a lot of code 

echo $a strtotime('yesterday 00:00');

// in $a hour = 23:00:00 and you may not know about it
// https://onlinephp.io/c/ef696
// use DateTimeImmutable
2024-03-25 11:25:27
http://php5.kiev.ua/manual/ru/function.strtotime.html

    Поддержать сайт на родительском проекте КГБ