PHP Date Function

Posted on Updated on

rtetetetePHP Date Function

The PHP date() function show the date and time.

The PHP date() function convert a timestamp to a more readable date and time.

Syntax

date(format,timestamp)

Note    :  A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).

List of keywords which are use for date.

d – Represents the day of the month (01 to 31)

m – Represents a month (01 to 12)

Y – Represents a year (in four digits)

l (lowercase ‘L’) – Represents the day of the week

Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.

Way to show different format  Today’s date :

Example

<?php

echo “Today is ” . date(“Y/m/d”) . “<br>”;

echo “Today is ” . date(“Y.m.d”) . “<br>”;

echo “Today is ” . date(“Y-m-d”) . “<br>”;

echo “Today is ” . date(“l”);

?>

date() function to automatically update the copyright year on your website:

Example

&copy; 2010-<?php echo date(“Y”)?>

Get a Simple Time

Some keywords  are commonly used for times:

h – 12-hour format of an hour with leading zeros (01 to 12)

i – Minutes with leading zeros (00 to 59)

s – Seconds with leading zeros (00 to 59)

a – Lowercase Ante meridiem and Post meridiem (am or pm)

Current time in the specified format:

Example

<?php

echo “The time is ” . date(“h:i:sa”);

?>

Note    :  Note that the PHP date() function will return the current date/time of the server!

Get Your Time Zone

If the display time is not right, it’s probably because your server is in another country or set up for a different time zone. So, if you want to get time of a specific location, you can set a timezone to use.

The example below sets the timezone to “Asia/Kolkata”:

Example

<?php

date_default_timezone_set(“Asia/Kolkata”);

echo “The time is ” . date(“h:i:sa”);

?>

Create a Date With PHP mktime()

The optional timestamp parameter in the date() function specifies a timestamp.

Syntax

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

It will creates a date and time from a number of parameters in the mktime() function:

Example

<?php

$d=mktime(11, 14, 54, 8, 12, 2014);

echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);

?>

strtotime() function is used to convert a human readable string to a Unix time.

Syntax

strtotime(time,now)

The example display a date and time from the strtotime() function:

Example

<?php

$d=strtotime(“10:30pm April 15 2014”);

echo “Created date is ” . date(“Y-m-d h:i:sa”, $d);

?>

PHP  is clever about convert  a string to a date, so we can put  values like:

Example

<?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>”;

?>

The example display  outputs the dates for the next six Saturdays:

Example

<?php

$startdate = strtotime(“Saturday”);

$enddate = strtotime(“+6 weeks”,$startdate);

while ($startdate < $enddate) {

  echo date(“M d”, $startdate),”<br>”;

  $startdate = strtotime(“+1 week”, $startdate);

}

?>

The example display  outputs the number of days until 4th of July:

Example

<?php

$d1=strtotime(“July 04”);

$d2=ceil(($d1-time())/60/60/24);

echo “There are ” . $d2 .” days until 4th of July.”;

?>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s