PHP 8.3.4 Released!

date

(PHP 4, PHP 5, PHP 7, PHP 8)

dateFormatta un timestamp Unix

Descrizione

date(string $format, ?int $timestamp = null): string

Restituisce una stringa formattata in accordo con il formato della stringa data usando l'intero timestamp dato (timestamp Unix) o l'orario corrente se nessun timestamp è dato. In altre parole, timestamp è opzionale e di default prende il valore di time().

Avviso

I timestamp Unix non gestiscono i fusi orari. Utilizzare la classe DateTimeImmutable, e i suoi metodi di formattazione DateTimeInterface::format() per formattare le informazioni data/ora con un fuso orario allegato.

Elenco dei parametri

format

Formato accettato da DateTimeInterface::format().

timestamp

Il parametro opzionale timestamp è un integer timestamp Unix che ha come default l'ora locale attuale se un timestamp non è fornito. In altre parole, ha come default il valore di time().

Valori restituiti

Restituisce una stringa data formattata. Se viene utilizzato un valore non numerico per timestamp, viene restituito false e viene emesso un errore di livello E_WARNING.

Errori/Eccezioni

Ogni chiamata a una funzione data/ora genera un E_NOTICE se il time zone non è valido, e/o un messaggio E_STRICT o E_WARNING se si usano le impostazioni di sistema o la variabile d'ambiente TZ. Vedere anche date_default_timezone_set()

Log delle modifiche

Versione Descrizione
8.0.0 timestamp ora è nullable.

Esempi

Example #1 Esempi di date()

<?php
// imposta il fuso orario di default da utilizzare.
date_default_timezone_set('UTC');


// Stampa qualcosa di simile a: Monday
echo date("l");

// Stampa qualcosa di simile a: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

// Stampa: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

/* usa le costanti nel parametro format */
// stampa qualcosa di simile a: Wed, 25 Sep 2013 15:28:57 -0700
echo date(DATE_RFC2822);

// stampa qualcosa di simile a: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>

È possibile evitare che un carattere riconosciuto nella stringa formato venga espanso effettuando l'escape di esso anteponendo un backslash. Se il carattere con un backslash è già una sequenza speciale, potrebbe essere necessario effettuare l'escape anche del backslash.

Example #2 Effettuare l'escape dei caratteri in date()

<?php
// stampa qualcosa di simile a: Wednesday the 15th
echo date('l \t\h\e jS');
?>

È possibile utilizzare insieme date() e mktime() per trovare date nel futuro o nel passato.

Example #3 Esempio di date() e di mktime()

<?php
$tomorrow
= mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>

Nota:

Questo può essere più affidabile rispetto che semplicemente aggiungere o sottrarre il numero di secondi in un giorno o in un mese ad un timestamp a causa dell'ora legale.

Alcuni esempi della formattazione date(). Notare che si dovrebbe effettuare l'escape di qualsiasi altro carattere, come tutti quelli che attualmente hanno un significato speciale produrranno risultati indesiderati, e ad altri caratteri potrebbe essere assegnato un significato nelle versioni future di PHP. Quando si effettua l'escape, assicurarsi di usare gli apici singoli in modo da evitare che i caratteri come \n divengano nuove linee.

Example #4 Formattazione di date()

<?php
// Assumendo che oggi sia Il 10 Marzo, 2001, 5:16:18 pm, e che noi siamo nel
// Fuso Orario Mountain Standard Time (MST)

$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18
$today = date("Y-m-d H:i:s"); // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>

Per formattare le date in altri lingue, può essere utilizzata IntlDateFormatter::format() al posto di date().

Note

Nota:

Per generare un timestamp da una stringa che rappresenta la data, si può essere in grado di utilizzare strtotime(). Inoltre, alcuni database hanno delle funzioni per convertire i loro formati data in timestamp (come la funzione » UNIX_TIMESTAMP di MySQL).

Suggerimento

Il timestamp dell'inizio della richiesta è disponibile in $_SERVER['REQUEST_TIME'].

Vedere anche:

add a note

User Contributed Notes 1 note

up
0
griddstone at outlook dot com
5 days ago
There is a fine distinction between g, G, h, and H. All of them are for the hour of the day, but in four different formats.

g = 12-hour format, no leading zeros
G = 24-hour format, no leading zeros
h = 12-hour format, with leading zeros
H = 24-hour format, with leading zeros

<?php
date
("g:i", mktime(0,0,0,3,13,2024)) // -> 12:00
date("G:i", mktime(8,0,0,3,13,2024)) // -> 8:00
date("h:i", mktime(8,0,0,3,13,2024)) // -> 08:00
date("H:i", mktime(0,0,0,3,13,2024)) // -> 00:00
?>

Essentially, capital letter is 24-hour format, lower case is 12-hour format. 'h' has leading zeros, 'g' does not.
To Top