English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial básico do PHP

Tutorial avançado do PHP

PHP & MySQL

Manual de referência do PHP

Uso e exemplo da função strtotime() do PHP

Manual de funções de data e hora do PHP

A função strtotime() converte qualquer descrição de data e hora em timestamp Unix

Definição e uso

strtotime()A função aceita datas textuais com/Data do valor de tempo/String de tempo e converta-a em timestamp Unix.

Anotação: Se o ano for representado no formato de dois dígitos, o valor 0-69 será mapeado para 2000-2069valor 70-100 será mapeado para 1970-2000.
Anotação: Preste atenção ao m/d/y ou d-m-y no formato da data, se o separador for barra (/),use o m americano/d/y no formato. Se o separador for traço (-) ou ponto (.), use o d europeu-m-y no formato. Para evitar erros potenciais, você deve usar YYYY o mais possível-MM-DD no formato ou usando a função date_create_from_format().

Sintaxe

strtotime($time)

Parâmetro

Número sequencialParâmetros e descrição
1

time (obrigatório)

Este valor representa a data/String de tempo.

2

now (opcional)

Isso representa um timestamp, usado como base para calcular datas relativas.

Valor de retorno

A função strtotime() do PHP retorna o valor de timestamp de uma string de data específica. Se falhar, esta função retornará um valor booleanofalso.

Versão do PHP

Esta função foi inicialmente introduzida no PHP 4Introduzido na versão .0 e pode ser usado em todas as versões mais recentes.
PHP 5.3.0: O formato de tempo relativo atual, por exemplo, esta semana, a semana passada, a semana anterior, a próxima semana, especifica que uma semana vai de segunda-feira a domingo, em vez de usar a data atual relativa/Time before and after 7 days.
PHP 5.3.0: Now 24:00 is a valid format.
PHP 5.2.7: In earlier versions, if you requested a specific date in a month and that date was the first day of the month, it would incorrectly add a week to the timestamp returned, which has now been corrected.
PHP 5.1.0: If it fails, it returns FALSE (in earlier versions it returned -1), and added E_STRICT and E_NOTICE timezone errors.
PHP 5.0.2: Now correctly calculates "now" and other relative times based on the current time, not based on midnight of today.
PHP 5.0.0: Allows microseconds (but microsecond numbers are usually ignored).

Online example

The following examples demonstratestrtotime()Function usage-

<?php
   $str = strtotime("12 September 2009");
   print("Timestamp: "$str"); 
?>
Test and see‹/›

Output result

Timestamp: 1252713600

Online example

If passed“ now”If used as a parameter, this function returns the current timestamp

<?php
   $str = strtotime("now");
   print("Timestamp: "$str"); 
?>
Test and see‹/›

Output result

Timestamp: 1589369948

Online example

Now, let's call this method by passing various date values-

<?php
// Set time zone
date_default_timezone_set("PRC");
$time = strtotime("2020-01-18 08:08:08");  // Convert a specified date to a timestamp 
// Print the current time PHP_EOL newline character, compatible with different systems
echo $time, PHP_EOL;
 
// More examples
echo strtotime("now"), PHP_EOL;
echo strtotime("now"), PHP_EOL;
echo strtotime("10 September 2019"), PHP_EOL;
echo strtotime("+1 day"), PHP_EOL;
echo strtotime("+1 week"), PHP_EOL;
echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL;
echo strtotime("next Thursday"), PHP_EOL;
echo strtotime("last Monday"), PHP_EOL;
?>
Test and see‹/›

This will produce the following output-

1579306088
1596165501
1596165501
1568044800
1596251901
1596770301
1596957503
1596643200
1595779200

Online example

Formatted as YYYY-MM-DD, and output the date

<?php
   $timestamp = strtotime("February 15, 2015" );   
   print date('Y-m-d', $timestamp );
?>
Test and see‹/›

This will produce the following output-

2015-02-15