Data si Timpul

postat acum 4 ani de Stefanescu Mihai in categorie PHP

Datele sunt lucruri cu care lucram in fiecare zi incat ni se pare foarte usor, iar PHP-ul vine cu cateva instrumente care ne ajuta foarte mult la manipularea datelor.

Time Stamp si functia time():

Functia time() din PHP ne ofera data si timpul curente. Nu necesita niciun argument si returneaza un integer.

Numarul intreg returnat de aceasta functie reprezinta numarul de secunde trecute din 1 Ianuarie 1970.

<?php
echo time();
?>

Ne va da urmatorul rezultat:

1420804132

Acest tip de informatie este greu de inteles, dar PHP-ul ne ofera cateva unelte foarte bune de convertit timestamp-ul intr-un format usor de inteles.

Convertirea Time Stamp-ului cu getdate():

Functia getdate() accepta un parametru (time stamp-ul) si returneaza un array asociativ ce contine informatii despre data. Daca nu-i dai nici-un parametru va returna data curenta.

In urmatorul tabel sunt informatiile oferite de getdate() in acel array asociativ:

Cheie Descriere Exemplu
seconds Secunde trecute(0-59) 45
minutes Minute trecute(0-59) 27
hours Ora(0-23) 18
mday Ziua din luna(1-31) 7
wday Ziua din saptamana(0-6) 3
mon Luna din an (1-12) 9
year Anul 2007
yday Ziua din an(0-356) 249
weekday Ziua din saptamana Friday
 month Luna din an April
 0  timestamp 1420804132

Acum, ai control complet asupra datei si a timpului. Puteti formata data in orice format va doriti:

<?php
$date_array = getdate();
foreach ( $date_array as $key => $val ){
   echo "$key = $val<br />";
}

$data  = "Data de azi: ";
$data .= $date_array[mday] . "/";
$data .= $date_array[mon] . "/";
$data .= $date_array[year];

print $data;
?>

Rezultatul:

seconds = 27
minutes = 25
hours = 11
mday = 12
wday = 6
mon = 5
year = 2007
yday = 131
weekday = Saturday
month = May
0 = 1178994327
Data de azi: 12/5/2007

Convertiere Time Stamp cu date():

Functia date() returneaza un string cu data formatata intr-un anumit mod. Cu aceasta functie aveti control total asupra formatului datei.

date(format, timestamp);

Puteti folosi functia date() fara a mai da un timestamp si aceasta va folosi timestamp-ul curent. Orice alt caracter pasat in informatiile de formatare va fi inclus in valoarea returnata.

Urmatorul tabel contine toate informatiile necesare la formatare:

Format Descriere Exemplu
a ‘am’ sau ‘pm’ in litere mici pm
A ‘AM’ sau ‘PM’ in litere mari PM
d Ziua din luna, numar fara 0 in fata 15
D Ziua saptamanii (3 litere) Thu
F Numele lunii January
h Ora (Format de 12 ore cu 0 in fata) 12
H Ora (Format de 24 ore cu 0 in fata) 22
g Ora (Format de 12 ore – fara 0 in fata) 12
G Ora (Format de 24 ore – fara 0 in fata) 22
i Minute (0-59) 23
j Ziua din luna(fara 0 in fata) 20
l (L mic) Ziua din saptamana. Thursday
L An bisect 1 sau 0
m  Luna din an (numar cu zero in fata) 04
M Luna din an (3 litere) Jan
r Data in format FC 2822  Thu, 21 Dec 2007 18:07:09 +0200
n Luna din an(numar fara 0 in fata)  4
s Secunda 34
U timestamp  1420806736
y An 02
Y An 2007
z  Ziua din an (0-356) 314

Exemplu:

<?php
print date("m/d/y G.i:s<br>", time());
print "Azi este ";
print date("j of F Y, \a\\t g.i a", time());
?>

Rezultatul:

01/20/00 13.27:55
Azi este 20 of January 2000, at 1.27 pm

 

Sunt un tanar programator din Bucuresti ce lucreaza in PHP/Mysql (MySqli/PDO), Laravel, CodeIgniter, MySQL, PostgreSQL, Wordpress, HTML5/CSS3, Sass, Photoshop si multe altele.
Google+ Community Facebook Group
Acest articol a fost mutat de pe vechea platforma.
Pentru orice eroare aparuta la mutare va rog sa ma contactati!

Comentariu postate de Lanora la data de 14.05.2019
You should take part in a contest for one of the finest websites on the web. I am going to highly recommend this site!
Comentariu postate de Marilyn la data de 14.05.2019
I've been surfing on-line more than 3 hours lately, but I never found any attention-grabbing article like yours. It's pretty price sufficient for me. In my opinion, if all web owners and bloggers made good content as you probably did, the internet will probably be much more helpful than ever before.
Comentariu postate de Remap tuning ecu la data de 14.05.2019
An interesting discussion is definitely worth comment. I do think that you should publish more about this subject matter, it might not be a taboo matter but typically folks don't discuss such subjects. To the next! Cheers!!
Comentariu postate de Tekhattan la data de 14.05.2019
Thank you, I have recently been looking for info approximately this topic for a while and yours is the greatest I've discovered so far. However, what concerning the conclusion? Are you positive concerning the supply?
Comentariu postate de http://boppern.de la data de 14.05.2019
At this time it looks like BlogEngine is the top blogging platform available right now. (from what I've read) Is that what you are using on your blog?
Comentariu postate de Remap tuning ecu la data de 14.05.2019
Whats up very nice blog!! Man .. Excellent .. Wonderful .. I'll bookmark your web site and take the feeds additionally? I am satisfied to search out so many helpful information right here in the put up, we want work out more strategies in this regard, thanks for sharing. . . . . .
Comentariu postate de Remap tuning ecu la data de 14.05.2019
I think this is one of the most significant information for me. And i am glad reading your article. But wanna remark on few general things, The website style is wonderful, the articles is really great : D. Good job, cheers
Comentariu postate de http://www.jadepedia.com/ la data de 15.05.2019
What's Happening i'm new to this, I stumbled upon this I have discovered It positively helpful and it has helped me out loads. I hope to contribute & help different customers like its aided me. Great job.
Comentariu postate de www.snorefreezer.com la data de 15.05.2019
Heya i'm for the primary time here. I found this board and I in finding It really helpful & it helped me out much. I'm hoping to present one thing again and aid others such as you helped me.
Comentariu postate de memory foam pillow meaning la data de 15.05.2019
Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.
Comentariu postate de best type of memory foam pillow la data de 16.05.2019
Nice weblog right here! Additionally your web site loads up fast! What web host are you the usage of? Can I get your associate hyperlink on your host? I desire my web site loaded up as fast as yours lol
Comentariu postate de Nancy la data de 16.05.2019
An outstanding share! I have just forwarded this onto a co-worker who was doing a little research on this. And he in fact bought me dinner simply because I discovered it for him... lol. So let me reword this.... Thanks for the meal!! But yeah, thanks for spending the time to discuss this matter here on your web site.
Comentariu postate de atalacademy.com la data de 16.05.2019
I don't even know how I finished up right here, however I thought this post was great. I do not know who you might be but definitely you are going to a famous blogger for those who are not already. Cheers!
Comentariu postate de Home Improvement Blogs la data de 16.05.2019
This post is invaluable. When can I find out more?
Comentariu postate de binary options signals la data de 17.05.2019
Excellent post. I was checking constantly this blog and I am impressed! Very useful information specially the last part : ) I care for such information much. I was seeking this particular information for a very long time. Thank you and good luck.
Comentariu postate de binary options signals la data de 17.05.2019
Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?
Comentariu postate de free signals la data de 17.05.2019
Howdy fantastic blog! Does running a blog similar to this require a massive amount work? I have absolutely no expertise in programming but I was hoping to start my own blog soon. Anyways, should you have any recommendations or tips for new blog owners please share. I understand this is off topic however I just had to ask. Thanks!
Comentariu postate de Home Improvement Idea la data de 17.05.2019
Thank you, I have recently been looking for info approximately this subject for a long time and yours is the best I've discovered so far. However, what in regards to the conclusion? Are you certain in regards to the source?
Comentariu postate de Home Improvement Blogs la data de 17.05.2019
This information is worth everyone's attention. When can I find out more?
Comentariu postate de Eric la data de 17.05.2019
Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I will be subscribing to your feeds and even I achievement you access consistently rapidly.
Comentariu postate de Home Improvement Blogs la data de 17.05.2019
Hello everyone, it's my first visit at this website, and article is genuinely fruitful for me, keep up posting such posts.