W e b t u t o r i a l s

Define PHP

PHP Installation


PHP Case Sensitivity

PHP Variable

PHP Echo & Print

PHP Operators

PHP Receiving Input

PHP Decision Making

PHP Loops

PHP Jumping Statement

PHP Image Gallery

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 File Upload

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP-MySQL Connectivity

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

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 2019/01/21
echo "Today is ".date("d.m.Y"); Today is 21.01.2019
echo "Today is ".date("m-d-Y"); Today is 01-21-2019
echo "The time is ".date("h:i:s a"); The time is 03:35:20 am
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.

   echo date_default_timezone_get(); //Display the current time zone.
   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);

	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);
	$d1=strtotime("July 04");
	echo "There are " . $d2 ." days until 4th of July.";

PHP 5 Date/Time Functions

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


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

	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 date_interval_format() - Function is used to format the interval. (Ver:5.3)
	// %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.");

PHP date_sub() Function - subtracts some days, months, years, hours, minutes, and seconds from a date. Ver:5.3
	date_sub($date,date_interval_create_from_date_string("40 days"));
	echo date_format($date,"Y-m-d");