Pages - পৃষ্ঠাসমূহ

PHP date tutorial with example

When need to show date in webpages, what we do?. How to do this? For this not go far. PHP has more functions to show date in php. This post show the details about date in php.For this we need to learn the syntax of PHP date symbols. This post provide the list of php date symbols.
Hide Example Show Example

PHP date function

In phpto show date we need to learn about php date. In php there has a function to call date adn some functions to settings php date function. Now see this functions
  • date()
  • mktime()
  • date_default_timezone_set()
Now show date we create a example.
<?php
 echo date("Y/m/d") . \n";
 echo date("Y.m.d") . "\n";
 echo date("Y-m-d");
?> 
here date functions has some parameter date.
  • date(Y/m/d) means date(Year/month/day)
  • date(Y.m.d) means date(Year.month.day)
  • date(Y-m-d) means date(Year-month-day)
See what meanings of this date parameters, a chart is listed below in the bottom.

Some examples of date() formatting.
<?php 
 // Mountain Standard Time (MST) Time Zone

 $date = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
 $date = date("m.d.y");                         // 03.10.01
 $date = date("j, n, Y");                       // 10, 3, 2001
 $date = date("Ymd");                           // 20010310
 $date = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
 $date = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
 $date = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
 $date = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
 $date = date("H:i:s");                         // 17:16:18
?>
If u can not understant waht time zone is it. You can define it by a php date function. A example is shown below.
<?php 
 echo(date_default_timezone_get());        //get the default time zone 
 date_default_timezone_set('UTC');    // set the default timezone to use. Available since PHP 5.1 
?>

mktime() function returns the Unix timestamp for a date. This timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax is shown below.
 mktime(hour,minute,second,month,day,year,is_dst) 
How to use it.
<?php
 $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
 echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
To show previousday, today and nextday we use mktime() function
 
<?php
 $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
 $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"),   date("Y"));
 $nextyear  = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
?>


Format of PHP date() function


Date functions
d

01 to 31

Day of the month, 2 digits with leading zeros
D

Mon, Tue, Wed, Thu, Fri, Sat, Sun

A textual representation of a day, three letters
j

1 to 31

Day of the month without leading zeros
l

Sunday through Saturday

(lowercase 'L') A full textual representation of the day of the week
z

0 through 365

The day of the year (starting from 0)
N

1 (for Monday) through 7 (for Sunday)

ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
S

st, nd, rd or th

English ordinal suffix for the day of the month, 2 characters
st (1st), nd (2nd), rd (3rd) or th (4th). Works well with j
w

0 (for Sunday) through 6 (for Saturday)

Numeric representation of the day of the week
week
W

42 (the 42nd week in the year)

ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
Month
F

January through December

A full textual representation of a month, such as January or March
m

01 through 12

Numeric representation of a month, with leading zeros
M Jan through Dec

A short textual representation of a month, three letters
n

1 through 12

n Numeric representation of a month, without leading zeros
t

28 through 31

Number of days in the given month
Year
L

1 leapyear
0 not leapyear

Whether it's a leap year 1 if it is a leap year, 0 otherwise.
o

1999 or 2003

ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
Y

1999 or 2010

A full numeric representation of a year, 4 digits
y

99 or 10

A two digit representation of a year
Time
a

am or pm

a Lowercase Ante meridiem and Post meridiem
A

AM or PM

Uppercase Ante meridiem and Post meridiem
B

000 through 999

Swatch Internet time
g

1 through 12

12-hour format of an hour without leading zeros
G

0 through 23

24-hour format of an hour without leading zeros
h

01 through 12

h 12-hour format of an hour with leading zeros
H

00 to 24

24 hours
i

00 to 59

Minutes with leading zeros
s

00 through 59

Seconds, with leading zeros
u

654321

Microseconds (added in PHP 5.2.2)
Time Zone
e

UTC, GMT, Atlantic/Azores

Timezone identifier (added in PHP 5.1.0)
I

1 if Daylight Saving Time, 0 otherwise.

(capital i) Whether or not the date is in daylight saving time
O

+0200

Difference to Greenwich time (GMT) in hours
P

+02:00

Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
T

EST, MDT ...

Timezone abbreviation
Z

-43200 through 50400

Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
Full Date / Time
c

2004-02-12T15:19:21+00:00

ISO 8601 date (added in PHP 5)
r

Thu, 21 Dec 2000 16:01:07 +0200

RFC 2822 formatted date
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) See also time()

No comments:

Post a Comment