Grazie alle numerose funzioni native dedicate alle date è possibile compiere molte operazioni sulle date e ottenere informazioni utili.
Queste che seguono sono alcune delle funzioni che non usano necessariamente un argomento. Queste usate semplicemente senza argomento restituiscono la data e/o il tempo corrente.
Qui di seguito alcune delle funzioni che si mostrano particolarmente utili perchè, usate con altre funzioni, permettono di lavorare su date ed orari ottenendo risultati interessanti
I tipi che si possono utilizzare associate alle molteplici funzioni native |
Note |
SECOND |
<?php $Query = "SELECT SECOND('21:29:46') AS test1, SECOND('2014-04-28 23:28:09') AS test2, SECOND(NOW()) AS test3, SECOND(CURTIME()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
46 |
9 |
24 |
24 |
|
estrae i secondi |
MINUTE |
<?php $Query = "SELECT MINUTE('21:29:46') AS test1, MINUTE('2014-04-28 23:28:09') AS test2, MINUTE(NOW()) AS test3, MINUTE(CURTIME()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
29 |
28 |
36 |
36 |
|
estrae i minuti |
HOUR |
<?php $Query = "SELECT HOUR('21:29:46') AS test1, HOUR('2014-04-28 23:28:09') AS test2, HOUR(NOW()) AS test3, HOUR(CURTIME()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
21 |
23 |
8 |
8 |
|
estrae le ore |
DAY |
<?php $Query = "SELECT DAY('21:29:46') AS test1, DAY('2014-04-28 23:28:09') AS test2, DAY(NOW()) AS test3 "; ?>
|
1 record restituiti
|
estrae i giorni
Il test1 è vuoto perchè è impossibile
estrarre il giorno da un orario |
DAYNAME |
<?php $Query = "SELECT DAYNAME(CURRENT_TIMESTAMP()) AS test1, DAYNAME('2014-04-28 23:28:09') AS test2, DAYNAME(NOW()) AS test3 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
Monday |
Monday |
Monday |
|
restituisce il nome della del giorno della settimana |
WEEK |
<?php $Query = "SELECT WEEK(CURRENT_TIMESTAMP()) AS test1, WEEK('2014-04-28 23:28:09') AS test2, WEEK(NOW()) AS test3 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
27 |
17 |
27 |
|
restituisce il numero di settimane trascorse per una data |
MONTH |
<?php $Query = "SELECT MONTH(CURRENT_TIMESTAMP()) AS test1, MONTH('2014-04-28 23:28:09') AS test2, MONTH(NOW()) AS test3 "; ?>
|
1 record restituiti
|
estrae i mesi |
MONTHNAME |
<?php $Query = "SELECT MONTHNAME(CURRENT_TIMESTAMP()) AS test1, MONTHNAME('2014-02-28 23:28:09') AS test2, MONTHNAME('2014-03-28 23:28:09') AS test3, MONTHNAME(NOW()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
July |
February |
March |
July |
|
restituisce il nome del mese |
QUARTER |
<?php $Query = "SELECT QUARTER(CURRENT_TIMESTAMP()) AS test1, QUARTER('2014-04-28 23:28:09') AS test2, QUARTER(NOW()) AS test3 "; ?>
|
1 record restituiti
|
restituisce il trimestre corrente |
YEAR |
<?php $Query = "SELECT YEAR(CURRENT_TIMESTAMP()) AS test1, YEAR('2014-04-28 23:28:09') AS test2, YEAR(NOW()) AS test3 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
2022 |
2014 |
2022 |
|
estrae l'anno |
LAST_DAY |
<?php $Query = "SELECT LAST_DAY(CURRENT_TIMESTAMP()) AS test1, LAST_DAY('2014-02-28 23:28:09') AS test2, LAST_DAY('2014-03-28 23:28:09') AS test3, LAST_DAY(NOW()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
2022-07-31 |
2014-02-28 |
2014-03-31 |
2022-07-31 |
|
restituisce l'ultimo giorno del mese per la data in esame in formato YYY-MM-DD |
|
<?php $Query = "SELECT DAY(LAST_DAY(CURRENT_TIMESTAMP())) AS test1, DAY(LAST_DAY('2014-02-28 23:28:09')) AS test2, DAY(LAST_DAY('2014-03-28 23:28:09')) AS test3, DAY(LAST_DAY(NOW())) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
31 |
28 |
31 |
31 |
|
una variante per ottenere il solo giorno |
TO_DAYS |
<?php $Query = "SELECT TO_DAYS('2007-07-03') AS from_2007, TO_DAYS('2014-03-03') AS from_2014, TO_DAYS('2010-07-03') AS from_2010, TO_DAYS(NOW()) AS ad_oggi "; ?>
|
1 record restituiti
from_2007 |
from_2014 |
from_2010 |
ad_oggi |
733225 |
735660 |
734321 |
738705 |
|
|
WEEKDAY() |
<?php $Query = "SELECT WEEKDAY(CURRENT_TIMESTAMP()) AS test1, WEEKDAY('2014-02-28 23:28:09') AS test2, WEEKDAY('2014-03-28 23:28:09') AS test3, WEEKDAY(NOW()) AS test4 "; ?>
|
1 record restituiti
test1 |
test2 |
test3 |
test4 |
0 |
4 |
4 |
0 |
|
restituisce l'indice del giorno della settimana |