[PHP] Date and date functions

Dieser Beitrag ist auch verfügbar in: German

Hi,

let’s talk today about date functions in PHP.

If you are looking for an interactive clock for your homepage, then you should look into Javascript functions. That is not possible with PHP. PHP is a server side language and can’t interact direct with the browser. Ok, you could try to play around with AJAX and reloads, but this is hard and why not using easier things…

But back to the subject. Date and PHP. I’ll show you some functions of the date class of PHP. The most important is date().

Date() return an formated date. It can be the current date or a date from an variable. For example it can be a timestamp from an database field. Also can be provided a format for the date. Just date or just time or both… and in various formats.
string <strong>date</strong> ( string <code>$format [, int $timestamp = time() ] )

Another, often used, function is mktime(). This command simply returns a UNIX timestamp. This timestamp can be saved in a database or can be used for whatever. You can provide also a date to get the timestamp for it, for example 01-03-1998.
I use it mostly without, to get the current timestamp.

int <strong>mktime</strong> ([ int <code>$hour = date(“H”) [, int $minute = date(“i”) [, int $second = date(“s”) [, int $month = date(“n”) [, int $day = date(“j”) [, int $year = date(“Y”) [, int $is_dst = -1 ]]]]]]] )

And with strtotime() you get the timestamp of a date which is saved in a variable. For example you got from somewhere the string “2000-07-01T00:00:00+00:00” and you need the timestamp for this date (in a string variable). Then use strtotime() and you’ll get it and can be saved into a database or whatever.

int <strong>strtotime</strong> ( string <code>$time [, int $now ] )

These are my 3 most used date function in PHP. There are at all 50 and more. If you like to know more about them, please have a look at php.net.

I hope that could help you a bit.

Best regards
Gordon

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*