Archive for the 'PHP' > 'DateTime' Category
The following is a simple method to set your timezone within PHP. You need to do this at the start of each of your pages. If your using OOP (Object-Oriented Programming) in PHP then put this in one of your main objects. Otherwise just include the line at the start of each file. If thats not possible, just include it at the top of any file that uses the built in date or time functions.
<?php
echo "Original Time: ". date("h:i:s") ."n";
date_default_timezone_set("Europe/London");
echo "New Time: ". date("h:i:s") ."n";
?>
As you can see, you need to use the
date_default_timezone_set() function along with a timezone identified.
You can find a list of valid timezones at
http://uk2.php.net/manual/en/timezones.php
Posted by epic at 16:03pm Permalink
So, heres a function to count the days between two dates. Its pretty easy to understand and very easy to use.
Ive added a few comments but its important to know, the strtotime function
only works on american dates, eg: MM/DD/YY and NOT DD/MM/YY like us English use...
function count_days($a, $b)
{
//Convert times to string format//
$datea = strtotime($a);
$dateb = strtotime($b);
//Use getdate on the strings (explodes them into array form//
//See manual for specific array column names//
$a_dt = getdate($datea);
$b_dt = getdate($dateb);
//Uses maketime to generate a numeric value for the date//
//Ive used 12, 0, 0 to specify midday every day (Using a constant time value)//
$a_new = mktime(12,0 ,0, $a_dt['mon'], $a_dt['mday'], $a_dt['year']);
$b_new = mktime(12,0 ,0, $b_dt['mon'], $b_dt['mday'], $b_dt['year']);
//Returns a rounded value devided by number of seconds in the day//
return round(($a_new - $b_new) / 86400);
}
Hope its of some use to someone.
Regards
Ollie
Posted by OLLIE at 18:39pm Permalink