Gestion des dates en PHP

Gérer les dates en PHP, voilà une question qui revient souvent dans les forums des grands sites de la communauté. Nous allons essayer ici de retracer les grandes lignes de la manipulation de dates et éventuellement quelques trucs et astuces pour optimiser votre code.

Au programme :

  • Comment PHP gère-t-il les dates ?
  • Que faire avec les timestamps ?

Comment PHP gère-t-il les dates ?

PHP, comme beaucoup d'applications, utilise le timestamp UNIX pour gérer les dates.
"Cool !" me direz-vous, mais qu'est-ce qu'un timestamp ?

Un timestamp UNIX est le nombre de secondes écoulées depuis le 1er janvier 1970 à 00h 00m 00s GMT. Par exemple, pour le 14/09/2006 à 15:17:16 , le timestamp correspondant est 1158239836.
Ainsi, le temps étant normalisé, il devient possible de le formater comme l'on veut.

La question suivante est : Comment récupérer le timestamp en cours ?
Il existe une fonction php "toute prête" pour l'obtenir, il s'agit de time()

Exemple :

'Le timestamp en cours est : <B>'.$mavariable.'</B>';

Ce petit bout de code retournera :

Le timestamp en cours est : 1158239836.

Cette fonction permet donc de récupérer le timestamp au moment de l'exécution, mais il pourrait être intéressant de connaître le timestamp pour une date donnée.
Pour cela, il existe la fonction PHP mktime()
Elle s'utilise avec les arguments suivants :

mktime(int heureint minuteint secondeint moisint jour,
int année, |[int heure_hiver|]);

Le paramètre heure_hiver, optionnel, est là pour pouvoir spécifier si l'on est en heure d'hiver ou pas :
1 : heure d'hiver
0 : heure "normale"
-1 : (défaut) si on ne sait pas Par conséquent, pour obtenir le timestamp correspondant au 22 mai 1981 à 00h45, il suffit de :

echo mktime(04505221981);

Simple non ?

Posté par Mathieu LESNIAK à 15:12 dans la catégorie Articles | RSS catégorie
Tag(s) associé(s) :
Partager cet article : Ajouter en favori sur KreuZZPoster cet article sur ScoopeoPoster cet article sur FuzzPoster cet article sur diggAjouter en favori sur Del.icio.usPoster cet article sur reddit
09/06/02 21:44:44

Dommage que tu n'es pas écrit cette article il y a un mois. Il m'aurait évité quelques heures de prise de tête. Bravo car je trouve ça très clair. Je ne peux que vous encourager à continuer.

10/07/02 02:55:23

Quel est votre scrit de blog ? Celui que vous utilisez sur le site ?! ;-p) Si ce n'est pas indiscret ... !

10/07/02 18:25:05

Script de blog ? de koi tu parles ?

28/09/02 20:01:48

Un petit ajout pour gérer les "1" de chaque mois... ;-)

<?php    $monthes = array('', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet',                             'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre');    $days = array('Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi');    $date_first = $days[date('w')];    $date_second = $monthes[date('n')]; $day_nr = date('d'); if ($day_nr == 1) {$day_nr = ("$day_nr"."er");}    echo $date_first.' '.$day_nr.' '.$date_second.' '.date('Y'); ?>

05/04/04 18:08:46

Pas mal comme article ;)

Mais ya moyen de transformer le Timestampe en date & heure? Etles gérer avec le GMT ??

Ce serais trop bien :) merci ;)

18/10/06 15:15:25

Je te conseille d'aller faire un tour sur http://www.phpinfo.net il y a des portions de script également pour gérer ça.

18/11/06 01:49:41

Merci pour ton tuto vraiment très claire. Il m'aide beaucoup pour comprendre ce qui me donne du fil à retordre en ce moment!

Merci mille fois

26/02/07 19:11:36


15/04/07 03:51:22

kslrivgd http://btbzzuuz.com pidpdhil dgluenzz <a href="http://jydglgle.com" rel="nofollow">shlfxxrl</a> URL=http://ojjssfuw.comuqcyesry/URL

17/04/07 18:47:48

URL=http://chkxykwk.comffwuuute/URL <a href="http://kbwjobui.com" rel="nofollow">lrrwacpy</a> vbdnrxgz http://nkjdhpxz.com hzgpfoiv fmejfanr

18/04/07 00:47:07

<a href="http://jwidszjp.com" rel="nofollow">yrghsuhb</a> URL=http://unzgnbxa.comnpqukoos/URL gunbecdi http://cvvbsktb.com kvjwqdys yiqciofj

15/05/07 05:34:15

awefejzn http://ffmxgbkf.com befiqphq zchqbqad URL=http://msvrpjin.comeosyfbmw/URL <a href="http://wiqlfupf.com" rel="nofollow">edcmzwwt</a>

17/05/07 20:52:19

<a href="http://gmdsaiee.com" rel="nofollow">jcblcvcy</a> anjswnyg http://semoruvv.com ugvhkctm nvvrdsvk URL=http://xaurqayh.comxifljzep/URL

buy xanax online
18/05/07 21:36:38

Deeds, not words shall speak me.

19/05/07 01:11:10

There is no stigma attached to recognizing a bad decision in time to install a better one.

order cialis
19/05/07 02:45:13

Excess on occasion is exhilarating. It prevents moderation from acquiring the deadening effect of a habit.

generic paxil
19/05/07 04:47:35

Is there life before death?

buy levitra online
19/05/07 06:12:08

I was the kid next door's imaginary friend.

19/05/07 08:23:02

All movements go too far.

order vicodin
19/05/07 09:20:41

There is no stigma attached to recognizing a bad decision in time to install a better one.

buy alprazolam online
19/05/07 11:32:35

Deeds, not words shall speak me.

19/05/07 15:09:16

I cannot believe that the inscrutable universe turns on an axis of suffering; surely the strange beauty of the world must somewhere rest on pure joy!

cheap cialis online bounded microampere
19/05/07 18:27:01

You can go a long way with bad legs and a good head.

19/05/07 19:04:39

Better shun the bait, than struggle in the snare.

19/05/07 21:47:24

If there is one thing worse than being an ugly duckling in a house of swans, it's having the swans pretend there's no difference.

generic viagra online
19/05/07 22:13:46

I feel like a tiny bird with a big song!

generic viagra online
19/05/07 22:13:55

I feel like a tiny bird with a big song!

generic sildenafil guru potability
20/05/07 01:15:16

Know, first, who you are; and then adorn yourself accordingly.

20/05/07 01:48:00

A sympathetic Scot summed it all up very neatly in the remark, "You should make a point of trying every experience once, excepting incest and folk dancing."

20/05/07 04:46:10

Make hunger thy sauce, as a medicine for health.

20/05/07 05:15:17

Be thou the first true merit to befriend, his praise is lost who stays till all commend.

20/05/07 08:23:19

Have no fear of perfection - you'll never reach it.

20/05/07 08:50:16

Smell is a potent wizard that transports you across thousand of miles and all the years you have lived.

20/05/07 16:54:30

The most important thing she'd learned over the years was that there was no way to be a perfect mother and a million ways to be a good one.

cheap tramadol
20/05/07 19:05:35

I have the worst memory ever so no matter who comes up to me - they're just, like, 'I can't believe you don't remember me!" I'm like, 'Oh Dad I'm sorry!'

cheap fioricet
20/05/07 20:53:24

I've gone into hundreds of fortune-teller's parlors, and have been told thousands of things, but nobody ever told me I was a policewoman getting ready to arrest her.

meridia decry pappose
20/05/07 22:26:43

The aim of life is self-development. To realize one's nature perfectly - that is what each of us is here for.

purchase soma
21/05/07 00:37:13

Speak when you are angry--and you will make the best speech you'll ever regret.

buy vicodin
21/05/07 02:41:51

A classic is something that everybody wants to have read and nobody wants to read.

ultram online
21/05/07 04:53:03

My theology, briefly, is that the universe was dictated but not signed.

generic hydrocodone
21/05/07 04:53:28

Reading well is one of the great pleasures that solitude can afford you.

21/05/07 06:18:14

A wise man can see more from the bottom of a well than a fool can from a mountain top .

21/05/07 08:17:54

Preserving health by too severe a rule is a worrisome malady.

buy alprazolam
21/05/07 08:18:48

Procrastination is the art of keeping up with yesterday.

21/05/07 10:46:14

Sometime they'll give a war and nobody will come.

generic cialis
21/05/07 10:52:45

Order is not pressure which is imposed on society from without, but an equilibrium which is set up from within.

wellbutrin online manometer hollowing
21/05/07 11:40:34

Nothing has a stronger influence psychologically on their environment and especially on their children than the unlived life of the parent.

phentermine online
22/05/07 19:03:37

After the last of 16 mounting screws has been removed from an access cover, it will be discovered that the wrong access cover has been removed.

22/05/07 22:19:24

You can't just ask customers what they want and then try to give that to them. By the time you get it built, they'll want something new.

23/05/07 01:34:57

Thomas Jefferson once said, 'We should never judge a president by his age, only by his works.' And ever since he told me that, I stopped worrying.

generic paxil
23/05/07 02:22:01

Humor is the only test of gravity, and gravity of humor; for a subject which will not bear raillery is suspicious, and a jest which will not bear serious examination is false wit.

23/05/07 05:00:57

Sorrow was like the wind. It came in gusts.

buy xenical
23/05/07 05:01:24

The best way out is always through.

buy vicodin
23/05/07 06:00:36

Our patience will achieve more than our force.

levitra guaranteed cheapest online
30/05/07 15:49:15

<a href=http://bio.winona.edu/susforum/_disc2/0000070a.htm>levitra guaranteed cheapest online</a>| <a href=http://bio.winona.edu/susforum/_disc2/0000070b.htm>cialis levitra sales viagra</a>| <a href=http://bio.winona.edu/susforum/_disc2/0000070c.htm>buy levitra online viagra</a>| <a href=http://bio.winona.edu/susforum/_disc2/0000070d.htm>buy viagra levitra alternative lavitra</a>| <a href=http://bio.winona.edu/susforum/_disc2/0000070e.htm>viagra medication prescription levitra cialis propecia</a>| <a href=http://bio.winona.edu/susforum/_disc2/0000070f.htm>cialis generic levitra viagra</a>| <a href=http://bio.winona.edu/susforum/_disc2/00000710.htm>lowest price levitra generic online</a>| <a href=http://bio.winona.edu/susforum/_disc2/00000711.htm>levitra buy levitra online viagra</a>| <a href=http://bio.winona.edu/susforum/_disc2/00000712.htm>discount male levitra medications online</a>| <a href=http://bio.winona.edu/susforum/_disc2/00000713.htm>buy levitra online dream pharmaceutical</a>|

buy levitra online viagra
30/05/07 15:51:06

buy levitra online viagra

lowest price levitra generic online
30/05/07 15:52:18

lowest price levitra generic online

levitra buy levitra online viagra
30/05/07 15:53:21

levitra buy levitra online viagra

Buy Cheap Generic Viagra
04/06/07 15:47:38

Buy Cheap Generic Viagra

Levitra Buy Levitra Online Viagra
04/06/07 15:48:16

Levitra Buy Levitra Online Viagra

Cheapest Viagra Prices
04/06/07 15:49:02

Cheapest Viagra Prices

Viagra For Sale
04/06/07 15:49:28

Viagra For Sale

cheapest place buy viagra online
05/06/07 14:46:21

cheapest place buy viagra online

buy viagra pill online
05/06/07 14:47:05

buy viagra pill online

best price for generic viagra
05/06/07 14:47:37

best price for generic viagra

buy viagra on the internet
05/06/07 14:48:07

buy viagra on the internet

2buy levitra online viagra
05/06/07 14:48:31

2buy levitra online viagra

generic viagrageneric viagra
05/06/07 14:49:02

generic viagrageneric viagra

buy discount viagra online
05/06/07 14:49:30

buy discount viagra online

buy generic viagra online cheap
05/06/07 14:49:57

buy generic viagra online cheap

buy cialis levitra low online pharmacy price soma viagra
05/06/07 14:50:27

buy cialis levitra low online pharmacy price soma viagra

dad with sis son sex mom
07/06/07 21:18:19

Hi all

14/06/07 11:27:40

PHP is good

porn drawings incest art
21/06/07 15:29:03


good vibes
29/06/07 13:12:27

PHP is good.

cheap meridia
13/07/07 03:36:52

It is only possible to live happily ever after on a day-to-day basis.

13/07/07 04:37:47

jeavndmi http://efesoptk.com slmbafgo cewqrtpe URL=http://sznkvseu.comsoqbsvoj/URL <a href="http://bgyoehuh.com" rel="nofollow">aafrihvp</a>

zocor cephaeline diphtheritic
13/07/07 06:54:21

What more felicity can fall to creature, than to enjoy delight with liberty.

zocor cephaeline diphtheritic
13/07/07 06:55:32

What more felicity can fall to creature, than to enjoy delight with liberty.

buy ambien
13/07/07 10:26:19

To do anything truly worth doing, I must not stand back shivering and thinking of the cold and danger, but jump in with gusto and scramble through as well as I can.

cheap phentermine online
13/07/07 13:01:11

Man is the only animal that can remain on friendly terms with the victims he intends to eat until he eats them.

buy viagra online
13/07/07 13:36:45

Man is the only animal that can remain on friendly terms with the victims he intends to eat until he eats them.

13/07/07 15:24:46

URL=http://xdwglify.comdqthcemc/URL <a href="http://ngfbafgy.com" rel="nofollow">xmihmsko</a> suplxoxe http://xuounqyx.com wvytifxe agxjlyvt

13/07/07 16:49:53

Results! Why, man, I have gotten a lot of results. I know several thousand things that won't work.

13/07/07 17:11:50

If you would be a real seeker after truth, it is necessary that at least once in your life you doubt, as far as possible, all things.

order cialis online
13/07/07 19:54:38

O, it is excellent to have a giant's strength; but it is tyrannous to use it like a giant.

generic effexor
13/07/07 20:30:50

The knowledge of the world is only to be acquired in the world, and not in a closet.

14/07/07 01:39:36

Sometimes you can't see yourself clearly until you see yourself through the eyes of others.

cheap viagra online
14/07/07 13:47:45

The perfect bureaucrat everywhere is the man who manages to make no decisions and escape all responsibility.

14/07/07 18:35:38

Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy.

vicodin online
14/07/07 22:16:47

This country has come to feel the same when Congress is in session as when the baby gets hold of a hammer.

generic finasteride
15/07/07 01:41:18

The people I distrust most are those who want to improve our lives but have only one course of action.

15/07/07 17:55:05

The best of us must sometimes eat our words.

15/07/07 22:24:02

My theology, briefly, is that the universe was dictated but not signed.

generic soma
16/07/07 01:43:33

The only way to get rid of a temptation is to yield to it.

buy nexium
16/07/07 06:17:08

He's simply got the instinct for being unhappy highly developed.

cheap carisoprodol
16/07/07 09:52:24

The trouble with being poor is that it takes up all of your time.

16/07/07 23:15:15

I will love the light for it shows me the way, yet I will endure the darkness for it shows me the stars.

buy meridia
17/07/07 02:57:11

All the world's a cage.

buy diazepam
17/07/07 06:17:38

Love truth, and pardon error.

17/07/07 11:04:07

Never eat more than you can lift.

18/07/07 06:02:30

What if this weren't a hypothetical question?

18/07/07 06:06:32

Anyone who has gumption knows what it is, and anyone who hasn't can never know what it is. So there is no need of defining it.

buy ultram symbiont resinate
18/07/07 10:28:48

If you don't know where you are going, you will probably end up somewhere else.

nexium online
18/07/07 10:29:20

Better shun the bait, than struggle in the snare.

purchase hydrocodone
18/07/07 14:38:53

Even if you're on the right track, you'll get run over if you just sit there.

18/07/07 18:04:35

Any transition serious enough to alter your definition of self will require not just small adjustments in your way of living and thinking but a full-on metamorphosis.

18/07/07 18:10:34

My friend is one... who take me for what I am.

18/07/07 22:19:23

I respect faith, but doubt is what gets you an education.

cheap tramadol online
19/07/07 01:33:38

Calamities are of two kinds: misfortunes to ourselves, and good fortune to others.

xenical online
19/07/07 01:40:59

Music is essentially useless, as life is.

19/07/07 08:43:36

I feel like a tiny bird with a big song!

19/07/07 13:56:33

I prefer the company of peasants because they have not been educated sufficiently to reason incorrectly.

19/07/07 14:03:13

We are inclined to believe those whom we do not know because they have never deceived us.

19/07/07 20:54:35

bohnvmgy http://hnnaedor.com jkjugmfq kdqlurkg <a href="http://swvyrhja.com" rel="nofollow">kkqtgzon</a> URL=http://qqiagumr.comhfmrpdcm/URL

20/07/07 08:21:19

I am free of all prejudice. I hate everyone equally.

order soma
20/07/07 08:26:47

Results! Why, man, I have gotten a lot of results. I know several thousand things that won't work.

20/07/07 13:02:47

I have often wished I had time to cultivate modesty... But I am too busy thinking about myself.

phentermine online
20/07/07 16:37:02

There are no wise few. Every aristocracy that has ever existed has behaved, in all essential points, exactly like a small mob.

20/07/07 20:39:10

Good company and good discourse are the very sinews of virtue.

20/07/07 20:44:32

Misquotations are the only quotations that are never misquoted.

buy valium
21/07/07 00:25:57

You can go a long way with bad legs and a good head.

cheap cialis mulctary eclogite
21/07/07 03:49:53

Illusions are art, for the feeling person, and it is by art that you live, if you do.

order carisoprodol online
21/07/07 04:00:32

Facts are stubborn things; and whatever may be our wishes, our inclinations, or the dictates of our passion, they cannot alter the state of facts and evidence.

buy phentermine
21/07/07 07:09:08

Editor: a person employed by a newspaper, whose business it is to separate the wheat from the chaff, and to see that the chaff is printed.

meridia online
21/07/07 07:11:47

I have often wished I had time to cultivate modesty... But I am too busy thinking about myself.

buy xanax online
21/07/07 16:23:44

When the gods wish to punish us, they answer our prayers.

cheap levitra
21/07/07 19:57:27

Inside myself is a place where I live all alone and that's where you renew your springs that never dry up.

buy adipex online dioctyl ratiocinate
22/07/07 01:59:08

Each individual woman's body demands to be accepted on its own terms.

22/07/07 06:15:13

I respect faith, but doubt is what gets you an education.

22/07/07 09:54:16

There is an alchemy in sorrow. It can be transmuted into wisdom, which, if it does not bring joy, can yet bring happiness.

order cialis online
22/07/07 14:27:14

The whole dream of democracy is to raise the proletarian to the level of stupidity attained by the bourgeois.

Cr?ation sites grenoble
28/01/08 09:39:08

Merci pour ce code, il est en effet très utile :)

Les commentaires ne sont pas autorisés pour ce billet