PHP- Date and Time



//To change date and time zone of apache server
open php.ini
date.timezone = Europe/Paris replace with
date.timezone = Asia/kolkata


or use   date_default_timezone_set("Asia/kolkata");


<meta http-equiv="refresh" content="1">


//To print all info
print_r(getdate())


//another example
$date_array = getdate();
foreach ( $date_array as $key => $val )
{
  print "$key = $val<br />";
}
$formated_date  = "Today's date: ";
$formated_date .= $date_array[mday] . "/";
$formated_date .= $date_array[mon] . "/";
$formated_date .= $date_array[year];


print $formated_date;


//To print date
<?php
echo “<br>”.date("m/d/y  H:i:s") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>


//converting Timestamp to date time etc


<?php
print date("m/d/y G.i:s<br>", time());
//05/11/13 14.19:43
print "Today is ";print date("j of F Y, \a\\t g.i a", time());
    //Today is 11 of 2013 May 2013, at 2.19 pm
?>

//To make own time

Syntax for mktime()

                     mktime(hour,minute,second,month,day,year)


To go one day in the future we simply add one to the day argument of mktime():


<?php
$tomorrow = mktime(0,0,0,date("m"),date("d"),date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>


//The output of the code above could be something like this:


output


Tomorrow is 2009/05/12
for($i=0;$i<=11;$i++){
$mon = date("M", mktime(0, 0, 0, $i+1, 0, 0, 0));
echo $mon;
}


O/P     DecJanFebMarAprMayJunJulAugSepOctNov


Facebook Month Example <select name="month"> <option>Month</option> <?php

//echo date("M",time()); for($i=2; $i<=13;$i++){ $str=date("M", mktime(0,0,0,$i,0,0)); echo "<option>$str</option>"; } ?> </select>



TIMER
$a=time();
// mktime(hour,minute,second,month,day,year,is_dst)
 $t=mktime(00,00,00,5,11,2013);
$t=$t-$a;
$s=$t%60;
$t/=60;//$t=$t/60
$m=$t%60;
$t/=60;
$h=$t%24;
print("$h:$m:$s");
how many hours are there between midnight of today's date (May 24, 2010) and midnight April 15, 2014?
$now = time();
$taxDeadline = mktime(0,0,0,4,15,2014);
// Difference in seconds
$difference = $taxDeadline - $now;
// Calculate total hours
$hours = round($difference / 60 / 60);
$days = round($difference / 60 / 60/24);
$year = round($difference / 60 / 60/24/365);
echo "Only ".number_format($hours)." hours until the tax deadline!";

Reactions: