PHP Date Functions

 

 


 

Dealing with Date and Time in PHP

 

In this article we discuss some widely used date functions in PHP.

Table of Contents

  •  date()
  •  getdate()
  •  strftime()
  •  time()
  •  maketime()

  
 **All dates only works when you run PHP in server.**


1. date() Function in PHP

 
 

The inbuilt PHP function date() is the most widely used method of returning date values.

 date_formats.php
 
 
<?php
 
$today = date('d-m-y'); 
 
print $today ."<br>";
 
$today = date('d m y');
 
print $today."<br>";
 
$today = date('d~m~y');
 
print $today ."<br>";
 
$today = date('D M Y');  
 
print $today ."<br>";
 
?> 
 
output: 
 
27-09-21
27 09 21
27~09~21
Mon Sep 2021

Time:


*** Current time depends on the timezone set in Apache. 

Timezone can be set as follows:

date_default_timezone_set('Asia/Kolkata'); 

To display current timezone, use:

date_default_timezone_get()

 

To find the timezone  of specific location use the following link:
  

Timezones

Changing time zone to print local time.

display_time.php

<?php 
 
 
print "<br>Current TimeZone: ". date_default_timezone_get();
 
date_default_timezone_set('Asia/Kolkata');  
 

print "<br>TimeZone: ". date_default_timezone_get();
 
$time = date('h:i:s a'); //current time 10:45:17 am
 
print "<br>Time: ".$time;
 
?> 
 
 
 
output:
 
Current TimeZone: Europe/Berlin
TimeZone: Asia/Kolkata
Time: 12:19:16 pm
 

 
 
Time in 24 hours format 

$time = date('G:i T'); 
 
print "<br>". $time;

output:

12:21 IST
 

G Hours (0 through 23)
T Timezone abbreviation ex: Examples: EST, MDT ...
i Minutes




2. strftime() Function in PHP

Format a local time/date according to locale settings
%c Preferred date and time stamp based on locale

print "<br>strftime=". strftime('%c');  
 
outputs:
 
09/27/21 12:22:50 


3. getdate() Function in PHP

 
<?php 
 
$date_array = getdate();
 
foreach ( $date_array as $key => $val
 
{
    print " $key = $val <br />";
}
    print ("<br>");
    print_r($date_array);
 
?> 
 
output:
 
seconds = 1
minutes = 55
hours = 8
mday = 27
wday = 1
mon = 9
year = 2021
yday = 269
weekday = Monday
month = September
0 = 1632725701

Array ( [seconds] => 1 [minutes] => 55 [hours] => 8 [mday] => 27 [wday] => 1 [mon] => 9 [year] => 2021 [yday] => 269 [weekday] => Monday [month] => September [0] => 1632725701 )

you can create custom date using  above array as shown below:

<?php
 
$date_array = getdate();
 
print "Today's date: ";
 
print $date_array['mday'] . "/". $date_array['mon'] . "/".$date_array['year'];
 

?>

output:

Today's date: 27/9/2021

4. time() Function in PHP

  

time(): function generates a time in long integer

 

<?php 
 
$t = time();
 
echo "<br />timestring :" .$t;  
 
echo "<br />time format" .date ("l, F jS", $t);

?>

output

timestring :1632729371
time formatMonday, September 27th

 

time format string:


week l (lowercase L),
month F,
date j (01 to 31)
date suffix S (nd,rd,th).

 

 

5. maketime() Function in PHP

 

If it is your preferred date, then you can generate a time stamp using mktime().


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

 

<?php 

$t = mktime(0, 0, 0, 1, 31, 2013); // custom datetime long integer
echo "<br /> datetime :".date ("l, F jS", $t);

?>
 
output:
datetime :Thursday, January 31st 


<?php 
 
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
echo "<br />Tomorrow :". $tomorrow; // 1377592773
echo "<br />Tomorrow :". date ("l, F jS Y ", $tomorrow);

$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
echo "<br />Lastmonth:". date ("l, F jS Y ",$lastmonth);  

$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
echo "<br />Nextyear:". date ("l, F jS Y ",$nextyear);  

?>


output:

Tomorrow :1632780000
Tomorrow :Tuesday, September 28th 2021
Lastmonth:Friday, August 27th 2021
nextyear:Tuesday, September 27th 2022




 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP