PHP Tutorial

Define PHP

PHP Installation

PHP INI File

PHP Comment

PHP Case Sensitivity

PHP Variable, DataType

PHP Echo & Print

PHP Operators

PHP Receiving Input

Decision Making - if...else

PHP Switch Case

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP File Upload

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP Assignment

Page Stats

Visitor: 406

PHP Date Functions

The PHP date() function is use to print the date and time in different formats.
Syntax: date(format, timestamp)
format (Required) - Specifies the format of the timestamp.
timestamp (Optional) - Specifies a timestamp. Default is the current date and time.

Format Description
d Print the day of the month (01 to 31)
m Print a month (1 to 12)
y Print a year (in two digits)
Y Print a year (in four digits)
M Print Month of Year, first three letters only. i.e. Aug
F Print a full month. i.e. August
D Print the day of the week. i.e. Sat
l (lowercase 'L') Print the day of the week. i.e. Saturday
H Display Hour (24-hour format)
h Display Hour (12-hour format) with leading zeros
i Display Minutes ( 0 - 59 )
s Display Seconds ( 0 - 59 )
a Dispaly Ante meridiem and Post meridiem, 'am' or 'pm' in lowercase
A Display Ante meridiem and Post meridiem 'AM' or 'PM' in upppercase
N Display numeric representation of a day (1 for Monday, 7 for Sunday)
L Display year is Leap year or not ('1' for yes, '0' for no)
z The day of the year (from 0 through 365)

Syntax: Display System Date and Time in Different formats.

Expression Output
echo "Today is ".date("Y/m/d"); Today is 2024/12/09
echo "Today is ".date("d.m.Y"); Today is 09.12.2024
echo "Today is ".date("m-d-Y"); Today is 12-09-2024
echo "The time is ".date("h:i:s a"); The time is 04:16:53 pm
echo "Today is ".date("l"); Today is Monday
Set the Time Zone

The PHP date() function will return the current date/time of the server. If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone. So, if you need the time to be correct according to a specific location, you can set a timezone.

<?php
   echo date_default_timezone_get(); //Display the current time zone.
   date_default_timezone_set("Asia/kolkata");
   echo "The time is " . date("h:i:sa");
?>

Some available Time Zone's are:

America/New_York America/Los_Angeles Africa/Casablanca Africa/Johannesburg
Antarctica/Casey Asia/Hong_Kong Asia/Singapore Asia/Dubai
Asia/Kolkata Asia/Tokyo Australia/Melbourne Australia/Sydney
Europe/Moscow Europe/Paris Indian/Mauritius Pacific/Auckland

Automatic Copyright Year: © 2016 - <?php echo date("Y")?>

Create a Date From a String With PHP strtotime()

$d = strtotime("15-1-2015");
echo "Created date is " . date("Y-m-d h:i:sa", $d);

$d = strtotime("2015-8-15");
echo "Created date is " . date("Y-m-d h:i:sa", $d);

$d = strtotime("10:30:23 2015-8-15");
echo "Created date is " . date("Y-m-d h:i:sa", $d);

$d = strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
<?php 
    $d=strtotime("tomorrow");        
    echo date("Y-m-d h:i:sa", $d) . "<br>";
    								 
    $d=strtotime("next Saturday");   
    echo date("Y-m-d h:i:sa", $d) . "<br>";
    								 
    $d=strtotime("+3 Months");       
    echo date("Y-m-d h:i:sa", $d) . "<br>";
?>

$startdate = strtotime("+1 week", $startdate);

<?php
	$d1=strtotime("July 04");
	$d2=ceil(($d1-time())/60/60/24);
	echo "There are " . $d2 ." days until 4th of July.";
?>

PHP checkdate() Function is used to validate a Gregorian date. Ver:4.0. Syntax: checkdate(month,day,year);

<?php
	var_dump(checkdate(12,31,-400));
	var_dump(checkdate(2,29,2003));
	var_dump(checkdate(2,29,2004));
?>

PHP date_add() Function adds some days, months, years, hours, minutes, and seconds to a date. Ver:5.3
Syntax: date_add(object,interval);

Example: Add 40 days to the 15th of March, 2015

<?php
	$date=date_create("2015-03-15");
	date_add($date,date_interval_create_from_date_string("40 days"));
	echo date_format($date,"Y-m-d");
?>

PHP date_diff() Function - returns the difference between two DateTime objects. Ver:5.3
Syntax date_diff(datetime1,datetime2,absolute);
Parameter Description
datetime1 Required. Specifies a DateTime object
datetime2 Required. Specifies a DateTime object
absolute Optional. Specifies a Boolean value. TRUE indicates that the interval/difference MUST be positive. Default is FALSE

Example: Calculate the difference between two dates:

<?php
	$date1=date_create("2013-03-15");
	$date2=date_create("2013-12-12");
	$diff=date_diff($date1,$date2);
?>

PHP date_interval_format() - Function is used to format the interval. (Ver:5.3)

<html>
   <body>
      <?php
         $date1=date_create("2013-01-01");
         $date2=date_create("2013-02-10");
         $diff=date_diff($date1,$date2);
         
         // %a outputs the total number of days
         echo $diff->format("Total number of days: %a.");
         echo "<br>";
         
         // %R outputs + beacause $date2 is after $date1 (a positive interval)
         echo $diff->format("Total number of days: %R%a.");
         echo "<br>";
         
         // %d outputs the number of days that is not already covered by the month
         echo $diff->format("Month: %m, days: %d.");
      ?>
   </body>
</html> 

PHP date_sub() Function - subtracts some days, months, years, hours, minutes, and seconds from a date. Ver:5.3

<html>
   <body>
      <?php
         $date=date_create("2013-03-15");
         date_sub($date,date_interval_create_from_date_string("40 days"));
         echo date_format($date,"Y-m-d");
      ?>
   </body>
</html>