PHP has multiple functions to get and format date and time. Some of the most used are:
date("format", timestamp)- format - A string format of the outputted date. Ex: "D, d M Y H:i:s" => "Mon, 15 Aug 2012 15:42:01 +0000"
$today = date('m/d/Y'); echo $today;By default, the date() function use the local server timezone, to set another default timezone, use the date_default_timezone_set('timezone_identifier'), prior to calling any of the date and time functions.
<?php date_default_timezone_set('Pacific/Auckland'); // set the default timezone: Pacific/Auckland // output the day of the week, of current time echo date('l'); // output the month, day and the year, of current time echo '<br />'. date('F j, Y'); // output the hour, minutes and seconds, of current time echo '<br />'. date('H:i:s'); ?>Outputs something like:
To get the current timestamp, use the time() function (i.g $ts = time(); ).
mktime(hour, minute, second, month, day, year)Returns the Unix timestamp corresponding the arguments given.
strtotime("time")- The "time" parameter is a date/time string, in english literary form (i.g "now", "+1 Day", "15 October 2012", "next Sunday", etc.). A list of valid formats is available in the Date and Time Formats.
<?php // get timestamp of October 15, 1976, 20:38:00 (with mktime) // output the timestamp and the day of the week of this date $stamp = mktime(20, 38, 0, 10, 15, 1976); echo 'Timestamp: '. $stamp; echo '<br /> October 15, 1976 is a '. date('l', $stamp); // Example with strtotime() echo '<br /><br />Example with strtotime():<br />'; echo '<br /> 15 October 1976, 20:38:00 - '. strtotime('15 October 1976, 20:38:00'); echo '<br /> 07 July 1996 - '. strtotime('07 July 1996'); echo '<br /> +3 days - '. strtotime('+3 days'); echo '<br /> +1 one week - '. strtotime('+1 week'); echo '<br /> +1 week 2 days 3 hours 30 seconds - '. strtotime('+1 week 2 days 3 hours 30 seconds'); echo '<br /> next Sunday - '. strtotime('next Sunday'); ?>This code will output:
The 0 day of a month reprezent the last day of the previous month.
For example, 0 day of March 2012 is "29" the last day of February 2012:
echo date('d', mktime(0, 0, 0, 03, 0, 2012)); // 29
getdate(timestamp)- timestamp - An integer Unix timestamp. It's optional. If not added, defaults to the current local time.
<?php $now = getdate(); echo '<pre>'; var_export($now); echo '</pre>'; ?>This code will output:
array ( 'seconds' => 48, 'minutes' => 30, 'hours' => 9, 'mday' => 21, 'wday' => 1, 'mon' => 3, 'year' => 2011, 'yday' => 79, 'weekday' => 'Monday', 'month' => 'March', 0 => 1300692648, )
<?php $load_time = -microtime(true); // php code $load_time += microtime(true); echo $load_time. ' seconds'; ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net