{"id":273,"date":"2016-07-28T14:25:26","date_gmt":"2016-07-28T14:25:26","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/28\/date-function-php\/"},"modified":"2024-03-03T15:00:12","modified_gmt":"2024-03-03T15:00:12","slug":"date-function-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/date-function-php\/","title":{"rendered":"Date function in PHP"},"content":{"rendered":"<h2>What is Date function in PHP ?<\/h2>\n<p>The date() function in PHP \u00a0formats a local date and time, and returns the formatted date string.<\/p>\n<p>Syntax :date(format,timestamp);<\/p>\n<p>for example<\/p>\n<pre><code>    \n\n&lt;?php\necho date(\\\"Y\/m\/d\\\") . \\\"&lt;br&gt;\\\";\necho \u00a0date(\\\"Y.m.d\\\") . \\\"&lt;br&gt;\\\";\necho date(\\\"Y-m-d\\\") . \\\"&lt;br&gt;\\\";\necho \u00a0date(\\\"l\\\");\n?&gt;\n <\/code><\/pre>\n<p>output will be:<\/p>\n<p>2016\/07\/28<br \/>\n2016.07.28<br \/>\n2016-07-28<br \/>\nThursday<\/p>\n<table class=\"w3-table-all notranslate\">\n<tbody>\n<tr>\n<th>Parameter<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>format<\/td>\n<td>Required. Specifies the format of the outputted date string. The following characters can be used:<\/p>\n<ul>\n<li>d &#8211; The day of the month (from 01 to 31)<\/li>\n<li>D &#8211; A textual representation of a day (three letters)<\/li>\n<li>j &#8211; The day of the month without leading zeros (1 to 31)<\/li>\n<li>l (lowercase \\&#8217;L\\&#8217;) &#8211; A full textual representation of a day<\/li>\n<li>N &#8211; The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)<\/li>\n<li>S &#8211; The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)<\/li>\n<li>w &#8211; A numeric representation of the day (0 for Sunday, 6 for Saturday)<\/li>\n<li>z &#8211; The day of the year (from 0 through 365)<\/li>\n<li>W &#8211; The ISO-8601 week number of year (weeks starting on Monday)<\/li>\n<li>F &#8211; A full textual representation of a month (January through December)<\/li>\n<li>m &#8211; A numeric representation of a month (from 01 to 12)<\/li>\n<li>M &#8211; A short textual representation of a month (three letters)<\/li>\n<li>n &#8211; A numeric representation of a month, without leading zeros (1 to 12)<\/li>\n<li>t &#8211; The number of days in the given month<\/li>\n<li>L &#8211; Whether it\\&#8217;s a leap year (1 if it is a leap year, 0 otherwise)<\/li>\n<li>o &#8211; The ISO-8601 year number<\/li>\n<li>Y &#8211; A four digit representation of a year<\/li>\n<li>y &#8211; A two digit representation of a year<\/li>\n<li>a &#8211; Lowercase am or pm<\/li>\n<li>A &#8211; Uppercase AM or PM<\/li>\n<li>B &#8211; Swatch Internet time (000 to 999)<\/li>\n<li>g &#8211; 12-hour format of an hour (1 to 12)<\/li>\n<li>G &#8211; 24-hour format of an hour (0 to 23)<\/li>\n<li>h &#8211; 12-hour format of an hour (01 to 12)<\/li>\n<li>H &#8211; 24-hour format of an hour (00 to 23)<\/li>\n<li>i &#8211; Minutes with leading zeros (00 to 59)<\/li>\n<li>s &#8211; Seconds, with leading zeros (00 to 59)<\/li>\n<li>u &#8211; Microseconds (added in PHP 5.2.2)<\/li>\n<li>e &#8211; The timezone identifier (Examples: UTC, GMT, Atlantic\/Azores)<\/li>\n<li>I (capital i) &#8211; Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)<\/li>\n<li>O &#8211; Difference to Greenwich time (GMT) in hours (Example: +0100)<\/li>\n<li>P &#8211; Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)<\/li>\n<li>T &#8211; Timezone abbreviations (Examples: EST, MDT)<\/li>\n<li>Z &#8211; Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)<\/li>\n<li>c &#8211; The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)<\/li>\n<li>r &#8211; The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)<\/li>\n<li>U &#8211; The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)<\/li>\n<\/ul>\n<p>and the following predefined constants can also be used (available since PHP 5.1.0):<\/p>\n<ul>\n<li>DATE_ATOM &#8211; Atom (example: 2013-04-12T15:52:01+00:00)<\/li>\n<li>DATE_COOKIE &#8211; HTTP Cookies (example: Friday, 12-Apr-13 15:52:01 UTC)<\/li>\n<li>DATE_ISO8601 &#8211; ISO-8601 (example: 2013-04-12T15:52:01+0000)<\/li>\n<li>DATE_RFC822 &#8211; RFC 822 (example: Fri, 12 Apr 13 15:52:01 +0000)<\/li>\n<li>DATE_RFC850 &#8211; RFC 850 (example: Friday, 12-Apr-13 15:52:01 UTC)<\/li>\n<li>DATE_RFC1036 &#8211; RFC 1036 (example: Fri, 12 Apr 13 15:52:01 +0000)<\/li>\n<li>DATE_RFC1123 &#8211; RFC 1123 (example: Fri, 12 Apr 2013 15:52:01 +0000)<\/li>\n<li>DATE_RFC2822 &#8211; RFC 2822 (Fri, 12 Apr 2013 15:52:01 +0000)<\/li>\n<li>DATE_RFC3339 &#8211; Same as DATE_ATOM (since PHP 5.1.3)<\/li>\n<li>DATE_RSS &#8211; RSS (Fri, 12 Aug 2013 15:52:01 +0000)<\/li>\n<li>DATE_W3C &#8211; World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00)<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td>timestamp<\/td>\n<td>Optional. Specifies an integer Unix timestamp. Default is the current local time (time())<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>time()<\/strong> give the number of \u00a0second\u00a0since 1 st jan 1970 00:00:01<\/p>\n<p>base date in <a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP <\/a>is Consider\u00a0 number of seconds since 1 st jan 1970 00:00:01<\/p>\n<p>To know how many seconds, minutes, hours, days, years are since is give as<\/p>\n<pre><code>   \n\n$s=time();\necho \\\"&lt;br\/&gt;Number of seconds is $s\\\";\n$m=$s\/60;\necho \\\"&lt;br\/&gt;Number of minutes is $m\\\";\n$h=$m\/60;\necho \\\"&lt;br\/&gt;Number of hours is $h\\\";\n$d=$h\/24;\necho \\\"&lt;br\/&gt;Number of days is $d\\\";\n$y=$d\/365;\necho \\\"&lt;br\/&gt;Number of years is $y\\\";  <\/code><\/pre>\n<h2>How to Validate Date \u00a0in PHP ?<\/h2>\n<p>to understand \u00a0take an example<\/p>\n<p>$d=29;<br \/>\n$m=2;<br \/>\n$y=2016;<\/p>\n<pre><code>  \n\nif(checkdate($m,$d,$y)) \/\/validates a date\necho \\\"&lt;br\/&gt;valid date\\\";\nelse\necho \\\"&lt;br\/&gt;Invalid date\\\";   <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Date function in PHP ? The date() function in PHP \u00a0formats a local date and time, and returns [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}