
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Archives - TECHNIG</title>
	<atom:link href="https://www.technig.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>https://168.138.42.164/tag/code/</link>
	<description>Gateway for IT Experts and Tech Geeks</description>
	<lastBuildDate>Sat, 25 Feb 2017 16:00:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://www.technig.com/wp-content/uploads/2020/04/32x32.png</url>
	<title>Code Archives - TECHNIG</title>
	<link>https://168.138.42.164/tag/code/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">162720667</site>	<item>
		<title>Formatting PHP Dates and Time: Step by Step Guide</title>
		<link>https://www.technig.com/formatting-php-dates-time/</link>
					<comments>https://www.technig.com/formatting-php-dates-time/#comments</comments>
		
		<dc:creator><![CDATA[Hujatulla Asghari]]></dc:creator>
		<pubDate>Sat, 25 Feb 2017 16:00:21 +0000</pubDate>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Function]]></category>
		<category><![CDATA[Programming]]></category>
		<guid isPermaLink="false">https://www.technig.com/?p=9057</guid>

					<description><![CDATA[<div style="margin-bottom:20px;"><img width="904" height="582" src="https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Formatting PHP Dates and Time" decoding="async" fetchpriority="high" srcset="https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time.jpg 904w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-300x193.jpg 300w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-768x494.jpg 768w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-210x136.jpg 210w" sizes="(max-width: 904px) 100vw, 904px" /></div>
<p>PHP is by far the most popular web programming language. Almost for every project that’s done in PHP, you work with date and times. Working and formatting PHP dates and times might be a little hard and complicated at first, but they will be interesting and useful, and you become more expert in PHP language. [&#8230;]</p>
<p>The post <a href="https://www.technig.com/formatting-php-dates-time/">Formatting PHP Dates and Time: Step by Step Guide</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div style="margin-bottom:20px;"><img width="904" height="582" src="https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Formatting PHP Dates and Time" decoding="async" srcset="https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time.jpg 904w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-300x193.jpg 300w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-768x494.jpg 768w, https://www.technig.com/wp-content/uploads/2017/02/Formatting-PHP-Dates-and-Time-210x136.jpg 210w" sizes="(max-width: 904px) 100vw, 904px" /></div><p>PHP is by far the most popular web programming language. Almost for every project that’s done in PHP, you work with date and times. Working and formatting PHP dates and times might be a little hard and complicated at first, but they will be interesting and useful, and you become more expert in PHP language. In this article, we focus on formatting PHP dates and time and how we can display dates in a variety of different styles.</p>
<h2>How to Create PHP Dates (Basic)</h2>
<p>Let’s imagine we create a date by ourselves. Of course, you might bring it from database in your real applications. So, to create date and time in PHP, all you need is to use<span style="background-color: #e3e3e3;"> the DateTime()</span> function.</p>
<pre class="theme:sublime-text lang:php decode:true">$date = new DateTime('2017-02-20');
</pre>
<p>If you want to use the procedural style.</p>
<pre class="theme:sublime-text lang:php decode:true">$date = date_create('2017-02-20');</pre>
<p>So, now we have the <span style="background-color: #e3e3e3;">$date </span>variable. You can’t directly echo the <span style="background-color: #e3e3e3;">$date</span> variable, because it’s not string. In order to convert it to string, you must use<span style="background-color: #e3e3e3;"> format()</span> function.</p>
<pre class="theme:sublime-text lang:php decode:true">echo $date-&gt;format('Y-m-d');</pre>
<p>Once again, if you use procedural style, you need to code in a different way.</p>
<pre class="theme:sublime-text lang:php decode:true">echo date_format($date, 'Y-m-d');</pre>
<p>Since then, we will write only OOP code.</p>
<p>The output of our code will be like this. (at the time I am writing this article).</p>
<pre class="lang:default decode:true">2017-02-20</pre>
<p>So now we have our date format.</p>
<h2>Formatting PHP Dates</h2>
<p>Let’s start formatting our date and make it more human readable. We can use <span style="background-color: #e6e6e6;">date() </span>and<span style="background-color: #e6e6e6;"> strtotime() </span>functions for formatting our date.  <span style="background-color: #e6e6e6;">date()</span> is used to format timestamps into a human readable string, and takes a formatting argument and an optional time argument. You can use <span style="background-color: #e6e6e6;">strtotime()</span> function to convert a date string into a timestamp. However, <span style="background-color: #e6e6e6;">strtotime()</span> doesn&#8217;t recognize the y-m-d-h-i-s format. So, you must pass a string format or give it human readable strings like: <em>now</em>, +1 week, next month, etc.</p>
<p>Simply, use <span style="background-color: #e6e6e6;">date()</span> function and give <span style="background-color: #e6e6e6;">strtotime()</span> as second parameter.</p>
<pre class="theme:sublime-text lang:php decode:true">echo date("r", strtotime("now"));</pre>
<blockquote><p>The “r” formatting string returns the time formatted as specified by RFC 2822. Of course, you can use other specifier to define your own custom formats.</p></blockquote>
<p>The output will show like this.</p>
<pre class="lang:default decode:true">Wed, 22 Feb 2017 18:03:55 +0000</pre>
<p>If you want to pass our own date that we have created, you can do that like this</p>
<pre class="theme:sublime-text lang:php decode:true">echo date('d M Y', strtotime($date-&gt;format('d-m-Y')));

// Output
// 20 Feb 2017</pre>
<p>You can check the list of all formats to display in<em> <a href="http://php.net/manual/en/function.date.php">php.net</a></em>. Here are some more examples</p>
<pre class="theme:sublime-text lang:php decode:true">echo date('F j, Y, g:i a', strtotime($date-&gt;format('d-m-Y')));

// Output
// February 20, 2017, 12:00 am

echo date('jS \of F Y', strtotime($date-&gt;format('d-m-Y')));

// Ouput
// 20th of February 2017</pre>
<p>&nbsp;</p>
<h2>Human Readable Date Time Using PHP</h2>
<p>If you learned formatting PHP dates, let&#8217;s step more forward and find out how to display more human readable dates in PHP.  You might have seen this before in any websites, that shows, <em>this article was posted three days ago or 2 hours ago.</em></p>
<blockquote><p>How would you change a date to show the difference of now and the specified date.</p></blockquote>
<p>We can create a function to do that for us. Here is the function.</p>
<pre class="theme:sublime-text lang:php decode:true ">function display_time_ago($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now-&gt;diff($ago);

    $diff-&gt;w = floor($diff-&gt;d / 7);
    $diff-&gt;d -= $diff-&gt;w * 7;

    $string = array(
        'y' =&gt; 'year',
        'm' =&gt; 'month',
        'w' =&gt; 'week',
        'd' =&gt; 'day',
        'h' =&gt; 'hour',
        'i' =&gt; 'minute',
        's' =&gt; 'second',
    );
    foreach ($string as $k =&gt; &amp;$v) {
        if ($diff-&gt;$k) {
            $v = $diff-&gt;$k . ' ' . $v . ($diff-&gt;$k &gt; 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
</pre>
<p>Now you can use display_time_ago() function and give it date.</p>
<p>Here is the example.</p>
<pre class="theme:sublime-text lang:php decode:true">$date = date_create('2017-02-20');
echo display_time_ago($date-&gt;format('d-m-Y'));

// 2 days ago</pre>
<h2>Conclusion</h2>
<p>We hope you have learned something new about formatting PHP dates and time. If you have any further questions, feel free to comment it bellow. 🙂</p>
<p>The post <a href="https://www.technig.com/formatting-php-dates-time/">Formatting PHP Dates and Time: Step by Step Guide</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.technig.com/formatting-php-dates-time/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">9057</post-id>	</item>
	</channel>
</rss>
