Sunteți pe pagina 1din 13

Gestionarea timpului in PHP

• Vom parcurge:

1.cum se obţine data şi ora curentă


2.cum se obţin informatii despre o dată
3.cum se formatează o dată
4.cum se testează validitatea datelor
5.cum se setează datele
Obţinerea datei cu time()
• time() returnează info despre data şi ora curentă.
• time() nu are nici un argument şi returnează un întreg care este dificil de
interpretat de om dar foarte util în aplicaţii.

<?php
print time();
// sample output: 948316201
?>

• Întregul returnat reprezintă numarul de secunde de la 1 ianuarie 1970.


• Acest moment se mai numeşte si UNIX epoch iar numarul de secunde de la acest
moment până în prezent se mai numeste şi „time stamp”(marca timpului).
Convertirea timpului cu getdate()
• getdate() are ca parametru opţional o timestamp şi returnează un
array asociativ ce conţine informaţii despre dată.

• Valoarea implicită a parametrului timestamp este timestamp-ul


curent returnat de time().

• Elemetele returnate de getdate() sunt date în tabela urmatoare:


Key Description Example returned values
Numeric representation
"seconds" 0 to 59
of seconds
Numeric representation
"minutes" 0 to 59
of minutes
Numeric representation
"hours" 0 to 23
of hours
Numeric representation
"mday" 1 to 31
of the day of the month
Numeric representation
"wday" 0 (for Sunday) through 6 (for Saturday)
of the day of the week
Numeric representation
"mon" 1 through 12
of a month
A full numeric
"year" representation of a year, Examples: 1999 or 2003
4 digits
Numeric representation
"yday" 0 through 366
of the day of the year
A full textual
"weekday" representation of the Sunday through Saturday
day of the week
A full textual
representation of a
"month" January through December
month, such as January
or March
Seconds since the Unix
Epoch, similar to the
0
values returned by
time()
• Exemplu:

<html>
<head>
<title>Obtinearea de informatii legate de timp cu getdate()</title>
</head>
<body>
<?php
$date_array = getdate(); // no argument passed so today's date will be used
foreach ( $date_array as $key => $val )
{
print "$key = $val<br>";
}
?>
<hr>
<?
print "Today's date: $date_array[mday]/$date_array[mon]/ $date_array[year]<p>";
?>
</body>
</html>
Convertirea timestamp cu date()
• getdate() se utilizeaza atunci când se intenţionează lucrul cu datele
returnate
• Uneori este necesară afişarea datei ca un string
• date() are un argument un şir ce specifică formatarea datei.
• Acest şir returnat poate fi formatat în foarte multe moduri.
• date() acceptă opţional şi un al doilea argument -- un time stamp.
• Tabela urmatoare conţine coduri posibile pentru formatarea datei.
format character Description Example returned values
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
c ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
A full textual representation of a month, such as January
F January through December
or March
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
I (capital i) Whether or not the date is in daylights savings time 1 if Daylight Savings Time, 0 otherwise.
j Day of the month without leading zeros 1 to 31
l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec

n Numeric representation of a month, without leading zeros 1 through 12


O Difference to Greenwich time (GMT) in hours Example: +0200
r RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
s Seconds, with leading zeros 00 through 59
English ordinal suffix for the day of the month, 2
S st, nd, rd or th. Works well with j
characters
t Number of days in the given month 28 through 31
T Timezone setting of this machine Examples: EST, MDT ...
Seconds since the Unix Epoch (January 1 1970 00:00:00
U See also time()
GMT)
• Exemplu:

<html>
<head>
<title>Listing 15.2 Formatting a date with date()</title>
</head>
<body>
<?php
print date("m/d/y G.i:s<br>", time());
// 01/20/00 13.27:55
print "Today is ";
print date("j of F Y, \a\\t g.i a", time());
// Today is 20 of January 2000, at 1.27 pm
?>
</body>
</html>
• Dacă se doreşte adăugarea unui string la la şirul se specifică codul de
formare, aceste caractere pot fi introduse scriind un backslash (\) în
faţa lor.
• Caracterele ce devin caractere de control când sunt precedate de un
backslash, trebuie precedate de două backdlash-uri:

"\n" trebuie să devină "\\n"

• date() returnează informatii corespunzatoare cu meridianul local.

• Dacă se doreşte formatarea unei date în format GMT, se utilizează


gmdate() care se comporta exact la fel ca şi date().
Crearea de timestamp-uri cu mktime()
• mktime() returnează o timestamp care poate fi utilizată cu date() sau
getdate().
• mktime() acceptă până la 6 argumente întregi în ordinea următoare:
• hour
• minute
• second
• month
• day of month
• year
<html>
<head>
<title>Crearea unui timestamp cu mktime()</title>
</head>
<body>
<?php
// make a timestamp for 1/5/99 at 2.30 am
$ts = mktime( 2, 30, 0, 5, 1, 1999 );
print date("m/d/y G.i:s<b\\r\>", $ts);
// 05/01/99 2.30:00
print "The date is ";
print date("j of F Y, \a\\t g.i a", $ts );
// The date is 1 of May 1999, at 2.30 am
?>
</body>
</html>

• Dacă se omit valori de parametrii în apelul funcţiei mktime(), acestea vor fi înlocuite cu valorile curente.
• mktime() va corecta automat valorile care sunt specificate în afara domeniului de definiţie. -- un argument
“hour 25” va deveni “hour 1.00 am”
Verificarea unei date cu checkdate()
• checkdate() verifică validitea unei date
• checkdate() are 3 argumente -- numere întregi:
1.month
2.day
3.Year

• checkdate() returnează true dacă month este între 1 şi 12, day este reală pentru luna şi anul dat.
• O dată poate fi validă dar inacceptabilă pentru alte funcţii de gestionare a timpului.
• checkdate( 4, 4, 1066 ) -- va returna true, iar
• mktime(4, 4, 1066) -- va returna − 1.
• REGULĂ: Nu se va utiliza mktime() cu ani mai mici decât 1902, şi se recomanda precauţie în
utilizarea funcţiei date cu argumente date mai mici decât 1970.

S-ar putea să vă placă și