{"id":15076,"date":"2020-06-18T11:59:29","date_gmt":"2020-06-18T11:59:29","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=15076"},"modified":"2021-11-05T06:05:34","modified_gmt":"2021-11-05T06:05:34","slug":"manipulation-of-dates-and-times-in-php-with-examples","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/manipulation-of-dates-and-times-in-php-with-examples\/","title":{"rendered":"Manipulation of dates and times in PHP with examples."},"content":{"rendered":"\n<p>Here we are going to discuss about the PHP Date and times. how can we call current date and time in our code. There are nearly fifty date and time functions, so for this tutorial we will narrow them down to some of them which are very important to us.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>Function<\/td><td>Description<\/td><\/tr><tr><td>date()<\/td><td>Formats a Local Time\/Date<\/td><\/tr><tr><td>time()<\/td><td>Returns the Current Time as a Unix Timestamp<\/td><\/tr><tr><td>strtotime()<\/td><td>Parses an English Textual Date or Time Into a Unix Timestamp<\/td><\/tr><tr><td>localtime()<\/td><td>Get the local time<\/td><\/tr><tr><td>datesunrise()<\/td><td>Returns time of sunrise for a given day and location<\/td><\/tr><tr><td>date_sunset()<\/td><td>Returns time of sunset for a given day and location<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">PHP Date() Function<\/h4>\n\n\n\n<p>The date() function formats a timestamp so that it actually makes sense, such as 4:58 PM Thursday, June 18, 2020. The <strong>date()<\/strong> function accepts two arguments, according to the following syntax: <strong>date(format, timestamp)<\/strong>.<\/p>\n\n\n\n<p><strong>Example Code :<\/strong> <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/6b8c2a1eb2872720b37fd119837f7aa4.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n\n<p><strong>date(<\/strong><em>format, timestamp<\/em><strong><em>)<\/em><\/strong><\/p>\n\n\n\n<p>PHP provides over thirty-five case-sensitive characters that are used to format the date and time. These characters are:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>&nbsp;<\/th><th>Character<\/th><th>Description<\/th><th>Example<\/th><\/tr><tr><td>Day<\/td><td>j<\/td><td>Day of the Month, No Leading Zeros<\/td><td>1 &#8211; 31<\/td><\/tr><tr><td>Day<\/td><td>d<\/td><td>Day of the Month, 2 Digits, Leading Zeros<\/td><td>01 &#8211; 31<\/td><\/tr><tr><td>Day<\/td><td>D<\/td><td>Day of the Week, First 3 Letters<\/td><td>Mon &#8211; Sun<\/td><\/tr><tr><td>Day<\/td><td>l (lowercase &#8216;L&#8217;)<\/td><td>Day of the Week<\/td><td>Sunday &#8211; Saturday<\/td><\/tr><tr><td>Day<\/td><td>N<\/td><td>Numeric Day of the Week<\/td><td>1 (Monday) &#8211; 7 (Sunday)<\/td><\/tr><tr><td>Day<\/td><td>w<\/td><td>Numeric Day of the Week<\/td><td>0 (Sunday) &#8211; 6 (Saturday)<\/td><\/tr><tr><td>Day<\/td><td>S<\/td><td>English Suffix For Day of the Month<\/td><td>st, nd, rd or th<\/td><\/tr><tr><td>Day<\/td><td>z<\/td><td>Day of the Year<\/td><td>0 &#8211; 365<\/td><\/tr><tr><td>Week<\/td><td>W<\/td><td>Numeric Week of the Year (Weeks Start on Mon.)<\/td><td>1 &#8211; 52<\/td><\/tr><tr><td>Month<\/td><td>M<\/td><td>Textual Representation of a Month, Three Letters<\/td><td>Jan &#8211; Dec<\/td><\/tr><tr><td>Month<\/td><td>F<\/td><td>Full Textual Representation of a Month<\/td><td>January &#8211; December<\/td><\/tr><tr><td>Month<\/td><td>m<\/td><td>Numeric Month, With Leading Zeros<\/td><td>01 &#8211; 12<\/td><\/tr><tr><td>Month<\/td><td>n<\/td><td>Numeric Month, Without Leading Zeros<\/td><td>1 &#8211; 12<\/td><\/tr><tr><td>Month<\/td><td>t<\/td><td>Number of Days in the Given Month<\/td><td>28 &#8211; 31<\/td><\/tr><tr><td>Year<\/td><td>L<\/td><td>Whether It&#8217;s a Leap Year<\/td><td>Leap Year: 1, Otherwise: 0<\/td><\/tr><tr><td>Year<\/td><td>Y<\/td><td>Numeric Representation of a Year, 4 Digits<\/td><td>1999, 2003, etc.<\/td><\/tr><tr><td>Year<\/td><td>y<\/td><td>2 Digit Representation of a Year<\/td><td>99, 03, etc.<\/td><\/tr><tr><td>Time<\/td><td>a<\/td><td>Lowercase Ante Meridiem &amp; Post Meridiem<\/td><td>am or pm<\/td><\/tr><tr><td>Time<\/td><td>A<\/td><td>Uppercase Ante Meridiem &amp; Post Meridiem<\/td><td>AM or PM<\/td><\/tr><tr><td>Time<\/td><td>B<\/td><td>Swatch Internet Time<\/td><td>000 &#8211; 999<\/td><\/tr><tr><td>Time<\/td><td>g<\/td><td>12-Hour Format Without Leading Zeros<\/td><td>1 &#8211; 12<\/td><\/tr><tr><td>Time<\/td><td>G<\/td><td>24-Hour Format Without Leading Zeros<\/td><td>0 &#8211; 23<\/td><\/tr><tr><td>Time<\/td><td>h<\/td><td>12-Hour Format With Leading Zeros<\/td><td>01 &#8211; 12<\/td><\/tr><tr><td>Time<\/td><td>H<\/td><td>24-Hour Format With Leading Zeros<\/td><td>00 &#8211; 23<\/td><\/tr><tr><td>Time<\/td><td>i<\/td><td>Minutes With Leading Zeros<\/td><td>00 &#8211; 59<\/td><\/tr><tr><td>Time<\/td><td>s<\/td><td>Seconds With Leading Zeros<\/td><td>00 &#8211; 59<\/td><\/tr><tr><td>Timezone<\/td><td>e<\/td><td>Timezone Identifier<\/td><td>Example: UTC, Atlantic<\/td><\/tr><tr><td>Timezone<\/td><td>I (capital i)<\/td><td>Whether Date Is In Daylight Saving Time<\/td><td>1 if DST, otherwise 0<\/td><\/tr><tr><td>Timezone<\/td><td>O<\/td><td>Difference to Greenwich Time In Hours<\/td><td>Example: +0200<\/td><\/tr><tr><td>Timezone<\/td><td>P<\/td><td>Difference to Greenwich Time, With Colon<\/td><td>Example: +02:00<\/td><\/tr><tr><td>Timezone<\/td><td>T<\/td><td>Timezone Abbreviation<\/td><td>Examples: EST, MDT &#8230;<\/td><\/tr><tr><td>Timezone<\/td><td>Z<\/td><td>Timezone Offset In Seconds<\/td><td>-43200 through 50400<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Using a combination of these characters and commas, periods, dashes, semicolons and backslashes, you can now format dates and times at which format you want.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/cfe1db3000b1af70c460eb9bd88ee0f2.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">PHP Time() Function<\/h4>\n\n\n\n<p>The time() function returns the current timestamp. The time() function can also return a modified timestamp. You can add or subtract any number of seconds to the function in order to return the timestamp of a previous or upcoming date and time.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n<p>time()<\/p>\n\n\n\n<p>For example, to return a timestamp for next week, or to return a timestamp from last week, I can add or subtract 7 days by determining how many seconds are involved (7 days * 24 hours per day * 60 minutes in an hour * 60 seconds in an hour = number of second in 7 days).<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/18718a7550a86acd92ad616b1a587116.js\"><\/script>\n\n\n\n<p>The result of above code will look like this:<\/p>\n\n\n\n<p>Last Week: 1591871848<br>Next Week: 1593081448<br>Next Month: 1595068648<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">PHP Strtotime() Function<\/h4>\n\n\n\n<p>The strtotime() function accepts an English datetime description and turns it into a timestamp. It is a simple way to determine &#8220;next week&#8221; or &#8220;last monday&#8221; without using the time() function and a bunch of math.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n<p>strtotime(time, now);<\/p>\n\n\n\n<p>Some examples are:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/96ca07c2ff02366ed64945864e9ac897.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">PHP localtime() function<\/h4>\n\n\n\n<p>The localtime() function returns an array that contains the time components of a Unix timestamp.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n<p>localtime(timestamp, is_assoc)<\/p>\n\n\n\n<p>Example Code : <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/54f218959e60a1df2800a71e5d4a413a.js\"><\/script>\n\n\n\n<p>The result will be look like this: <\/p>\n\n\n\n<p>Array ( [0] =&gt; 50 [1] =&gt; 2 [2] =&gt; 12 [3] =&gt; 18 [4] =&gt; 5 [5] =&gt; 120 [6] =&gt; 4 [7] =&gt; 169 [8] =&gt; 0 )<\/p>\n\n\n\n<p>Array ( [tm_sec] =&gt; 50 [tm_min] =&gt; 2 [tm_hour] =&gt; 12 [tm_mday] =&gt; 18 <br>[tm_mon] =&gt; 5 [tm_year] =&gt; 120 [tm_wday] =&gt; 4 [tm_yday] =&gt; 169 [tm_isdst] =&gt; 0 )<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">PHP datesunrise() Function<\/h4>\n\n\n\n<p>The date_sunrise() function returns the time of sunrise for a given day \/ location.&nbsp;<\/p>\n\n\n\n<p><strong>Note <\/strong>: It returns the time of the sunrise, in the specified format, on success. FALSE on failure.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n\n<p><strong>date_sunrise<\/strong>(<em>timestamp, format, latitude, longitude, zenith, gmtoffset)<\/em><\/p>\n\n\n\n<p>Example Code :<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/8c25277431f3dd622dbe5820b5d5c279.js\"><\/script>\n\n\n\n<p>the result will look like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"234\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/date-sunrise-code-result.jpg\" alt=\"\" class=\"wp-image-15105\"\/><figcaption>Date_sunrise Code Result<\/figcaption><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">PHP date_sunset() Function<\/h4>\n\n\n\n<p>The date_sunset() function returns the time of sunset for a given day \/ location.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Syntax<\/h4>\n\n\n\n<p><strong>date_sunset<\/strong>(<em>timestamp, format, latitude, longitude, zenith, gmtoffset)<\/em><\/p>\n\n\n\n<p>Example Code :<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/cmschandan\/73b1e9c2458cdd6a4e090f42be33414f.js\"><\/script>\n\n\n\n<p>The Result will look like this: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"349\" height=\"164\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/date-sunset-code-resultview.jpg\" alt=\"\" class=\"wp-image-15127\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/date-sunset-code-resultview.jpg 349w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/06\/date-sunset-code-resultview-300x141.jpg 300w\" sizes=\"auto, (max-width: 349px) 100vw, 349px\" \/><\/figure><\/div>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_28607\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_40635\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_28607\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Here we are going to discuss about the PHP Date and times. how can we call current date and time in our code. There are nearly fifty date and time functions, so for this tutorial we will narrow them down to some of them which are very important to us. Function Description date() Formats a&#8230;<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6144,6145,177],"class_list":["post-15076","post","type-post","status-publish","format-standard","hentry","category-php","tag-date-n-time","tag-localtime","tag-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=15076"}],"version-history":[{"count":9,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15076\/revisions"}],"predecessor-version":[{"id":24750,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/15076\/revisions\/24750"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=15076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=15076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=15076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}