Home > Archive by category 'Date'
 

default_timezone_set

Every call to a date/time function will generate a
E_NOTICE if the time zone is not valid,
and/or a E_STRICT or E_WARNING message if using the system settings
or the TZ environment variable.

Then this code needs to be added.
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
<pre>// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

?>

OUTPUT

Tuesday 3rd of May 2011 07:08:58 AM

Date Format

There are different formats to show the date in php.

  • d – Represents the day of the month (01 to 31)
  • m – Represents a month (01 to 12)
  • Y – Represents a year (in four digits)

Format-1

1
2
3
$today =  date("Y-M-D");
echo "Today's date is: " . $today. "<br />";

OUTPUT:

1
Today's date is: 2011-May-Tue

Format-2

1
2
3
$today =  date("y-m-d");
echo "Today's date is: " . $today. "<br />";

OUTPUT:

1
Today's date is: 11-05-03

Find Date of n days or n months or n years later from today using PHP

Current Date

1
2
3
$today =  date("Y-M-d");
echo "Today's date is: " . $today. "<br />";

OUTPUT :

Today’s date is: 2011-May-03

Two days later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m"),date("d")+2,date("Y"));
$lastdate=date("Y/M/d", $expire_date);
echo "Two days later from today= ".$lastdate;

OUTPUT would be like this:

1
Two days later from today= 2011/May/05

Two months later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m")+2,date("d"),date("Y"));
$lastdate=date("Y/M/d", $expire_date);
echo "Two months later from today= ".$lastdate;

OUTPUT would be like this:

1
Two months later from today= 2011/Jul/03

Two years later from today

1
2
3
4
5
$expire_date =mktime(0,0,0,date("m"),date("d"),date("Y")+2);
$lastdate=date("Y/M/d", $expire_date);
echo "Two years later from today= ".$lastdate;

OUTPUT would be like this:

1
Two years later from today= 2013/May/03


How to Find Date of Tomorrow, Next Month, Next Year using PHP

Another interesting and effective way to find date of certain day, month or year from today.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
     // TODAY'S DATE
     $today =  date("m-d-Y");
	 echo "Today's date is: " . $today. "<br />";

 	// 19 DAYS FROM TODAY
     $somedays = date("m-d-Y",strtotime("+19 days"));
	 echo "Date after 19 days from today: ". $somedays. "<br />";

     // 1 MONTH FROM TODAY
     $onemonth = date("m-d-Y",strtotime("+1 months"));
	 echo "Date after 1 month from today: " .$onemonth. "<br />";

     // 1 YEAR FROM TODAY
     $oneyear = date("m-d-Y",strtotime("+1 years"));
 	echo "Date after 1 year from today: ".$oneyear."<br />";
?>

Output would be like this.

1
2
3
4
5
6
7
Today's date is: 05-01-2011
Date after 19 days from today: 05-20-2011
Date after 1 month from today: 06-01-2011
Date after 1 year from today: 05-01-2012

Php code to find date after n days from today

We often face problem in finding date of say after 40 days from today. Since different month has different total days, so finding exact date is a challenge. For example January has 31 days, February has 28/29 days, April has 30 days. To sort out the problem you can follow below codes.

1
2
3
4
5
<?php
echo date("m/d/y");</span>
?>

would give you date in format 04/30/11 (04 is month April, 30 is date and 11 is year 2011)

Asian countries can use

1
<?php echo date("d/m/y"); // date/month/year format. ?>

Suppose we want to find date after 60 days from today (1st may 2011). To do this follow the code

1
2
3
<?php $tomorrow = mktime(0, 0, 0, date("m"), date("d")+60, date("y"));
echo "The date is ".date("d/m/y", $tomorrow); ?>

OUTPUT will be

1
The date is 30/06/11. (30 June 2011)

Now let’s do a little experiment. June has 30 days so let’s see the output of below code

1
2
3
<?php $tomorrow = mktime(0, 0, 0, date("m"), date("d")+61, date("y"));
echo "The date is ".date("d/m/y", $tomorrow); ?>

OUTPUT will be

1
The date is 01/07/11. (1 July 2011).

So it means by using mktime() and date() function of php we can find any date in future and we don’t have to worry about number of days in a month, leap-year.

 
 
 

Categories

Tag