
<?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>PHP Function Archives - TECHNIG</title>
	<atom:link href="https://www.technig.com/tag/php-function/feed/" rel="self" type="application/rss+xml" />
	<link>https://168.138.42.164/tag/php-function/</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>PHP Function Archives - TECHNIG</title>
	<link>https://168.138.42.164/tag/php-function/</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>
		<item>
		<title>5 Useful PHP Function for Working with Files</title>
		<link>https://www.technig.com/php-function-for-working-with-files/</link>
					<comments>https://www.technig.com/php-function-for-working-with-files/#respond</comments>
		
		<dc:creator><![CDATA[Hujatulla Asghari]]></dc:creator>
		<pubDate>Tue, 24 Mar 2015 19:33:15 +0000</pubDate>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Function]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web developement]]></category>
		<guid isPermaLink="false">https://www.technig.com/?p=1915</guid>

					<description><![CDATA[<div style="margin-bottom:20px;"><img width="640" height="426" src="https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Useful PHP Function" decoding="async" srcset="https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function.jpg 640w, https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function-300x200.jpg 300w, https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function-450x300.jpg 450w" sizes="(max-width: 640px) 100vw, 640px" /></div>
<p>PHP is definitely one of the most popular programming language for web developers. And this article is about 5 useful function that every PHP developer must know when working with files and directories. 1. file_put_contents — Write a string to a file This function is identical to calling fopen(), fwrite() and fclose() successively to write data [&#8230;]</p>
<p>The post <a href="https://www.technig.com/php-function-for-working-with-files/">5 Useful PHP Function for Working with Files</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div style="margin-bottom:20px;"><img width="640" height="426" src="https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Useful PHP Function" decoding="async" loading="lazy" srcset="https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function.jpg 640w, https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function-300x200.jpg 300w, https://www.technig.com/wp-content/uploads/2015/03/Useful-PHP-Function-450x300.jpg 450w" sizes="(max-width: 640px) 100vw, 640px" /></div><p>PHP is definitely one of the most popular programming language for web developers. And this article is about 5 useful function that every PHP developer must know when working with files and directories.</p>
<h3>1. <span class="refname">file_put_contents</span> — <span class="dc-title">Write a string to a file</span></h3>
<p>This function is identical to calling <span class="function"><a class="function" href="http://php.net/manual/en/function.fopen.php">fopen()</a></span>, <span class="function"><a class="function" href="http://php.net/manual/en/function.fwrite.php">fwrite()</a></span> and <span class="function"><a class="function" href="http://php.net/manual/en/function.fclose.php">fclose()</a></span> successively to write data to a file.  It mean if you use this function you don&#8217;t need to use three other functions like: fopen(),fwrite() and fclose().</p>
<p><strong>Usage : </strong></p>
<p>This function takes two arguments. The first one is for file name if doesn&#8217;t exist it will create one. The second argument is for data to be written in that file.</p>
<p><strong>Example</strong></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php file_put_contents("Technig.txt", "Hello world"); ?&gt;</pre>
<p>&nbsp;</p>
<p>You can pass an array as second argument</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php
 $data = array("Ali","Hamid", "Mahmod");
 file_put_contents("Technig.txt", $data);
 ?&gt;</pre>
<h3> 2. file_get_contents() reads a file into a string.</h3>
<p><strong>file_get_contents()</strong> function is similar to <span class="function"><a class="function" href="http://php.net/manual/en/function.file.php">file()</a></span>, except that <span class="function"><strong>file_get_contents()</strong></span> returns the file in a <span class="type"><a class="type string" href="http://php.net/manual/en/language.types.string.php">string</a></span>, starting at the specified <em><code>offset</code></em> up to <em><code>maxlen</code></em> bytes. On failure, it will return <strong><code>FALSE</code></strong>.</p>
<p>Usage:</p>
<p>This function takes five arguments.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">file_get_contents(path,include_path,context,start,max_length)</pre>
<p>First argument is required: specifies the file path to read.</p>
<p>Second argument is optional: Set this parameter to &#8216;1&#8217; if you want to search for the file in the include_path (in php.ini) as well.</p>
<p>Third argument is optional: Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. Can be skipped by using NULL.</p>
<p>Fourth argument is optional:  Specifies where in the file to start reading. This parameter was added in PHP 5.1</p>
<p>And Last Argument is also optional: Specifies how many bytes to read. This parameter was also added  in PHP 5.1</p>
<p><b>Tip:</b> <em>This function is binary-safe (meaning that both binary data, like images, and character data can be written with this function).</em></p>
<p><strong>Example:</strong></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php 
file_get_contents("Technig.txt");
 ?&gt;</pre>
<p>&nbsp;</p>
<h3>3.  glob() Function</h3>
<p>The glob() function returns an array of filenames or directories matching a specified pattern.</p>
<p>&nbsp;</p>
<p><strong>Usage:</strong></p>
<p>This function takes two arguments:</p>
<p>The first argument is pattern and it is required, specifies the pattern to search for.</p>
<p>The second argument is optional for some special settings.</p>
<blockquote><p>Possible values:</p>
<ul>
<li>GLOB_MARK &#8211; Adds a slash to each item returned</li>
<li>GLOB_NOSORT &#8211; Return files as they appear in the directory (unsorted)</li>
<li>GLOB_NOCHECK &#8211; Returns the search pattern if no match were found</li>
<li>GLOB_NOESCAPE &#8211; Backslashes do not quote metacharacters</li>
<li>GLOB_BRACE &#8211; Expands {a,b,c} to match &#8216;a&#8217;, &#8216;b&#8217;, or &#8216;c&#8217;</li>
<li>GLOB_ONLYDIR &#8211; Return only directories which match the pattern</li>
<li>GLOB_ERR &#8211; (added in PHP 5.1) Stop on errors (errors are ignored by default)</li>
</ul>
</blockquote>
<p><strong>Example :</strong></p>
<p>if you want to show all files with &#8220;.txt&#8221; extension in one directory.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false lang:php decode:true">&lt;?php
print_r(glob("*.txt"));
?&gt;</pre>
<p>The start (*) means whatever the name is but it must have &#8220;.txt&#8221; extension.</p>
<p>It will output an array with all file which has &#8220;.txt&#8221; extension.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">Array
(
[0] =&gt; Technig.txt
[1] =&gt; source.txt
[2] =&gt; file.txt
[3] =&gt; password.txt
)</pre>
<p>If you want to show all files with any extension you can do.  Simply use &#8220;*&#8221; as filename and extension.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php
print_r(glob("*.*"));
?&gt;</pre>
<p>Output:</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">Array
(
[0] =&gt; contacts.csv
[1] =&gt; default.php
[2] =&gt; Technig.txt
[3] =&gt; Data.txt
[4] =&gt; tem1.tmp
[5] =&gt; test.htm
[6] =&gt; test.ini
[7] =&gt; test.php
[8] =&gt; test.txt
)</pre>
<h3>4. basename() Function</h3>
<p>The basename() function returns the filename from a path.</p>
<p><strong>Usage:</strong></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">basename ($path, $suffix);</pre>
<p>&nbsp;</p>
<p>This function takes two arguments. The first one is required and specifies the path to check.</p>
<blockquote><p>On Windows, both slash (<em>/</em>) and backslash (<em>\</em>) are used as directory separator character. In other environments, it is the forward slash (<em>/</em>).</p></blockquote>
<p>The second argument is optional. Specifies a file extension. If the filename has this file extension, the file extension will not show.</p>
<p><strong>Example: </strong></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php
echo "
 0) ".basename("/Technig/test.d"); // file with extension 
echo "
 1) ".basename("/Technig/test.d", ".d"); // file without extension
echo "
 2) ".basename("/Technig/passwd"); 
echo "
 3) ".basename("/Technig/");
echo "
 4) ".basename(".");
echo "
 5) ".basename("/");
?&gt;</pre>
<p>Output</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">0) test.d
1) test
2) passwd
3) Technig
4) .
5)</pre>
<p>&nbsp;</p>
<h3>5. pathinfo() Function</h3>
<p>The pathinfo() function returns an array that contains information about a path.</p>
<p>The following array elements are returned:</p>
<ul style="list-style-type: circle;">
<li>[dirname]</li>
<li>[basename]</li>
<li>[extension]</li>
</ul>
<p><strong>Usage : </strong></p>
<p>This function also takes two arguments. The first argument is the path. It&#8217;s required. Specifies the path to check.</p>
<p>The second argument is the options. it&#8217;s optional. Specifies a specific element to be returned; one of <strong>PATHINFO_DIRNAME</strong>, <strong>PATHINFO_BASENAME</strong>, <strong>PATHINFO_EXTENSION</strong> or <strong>PATHINFO_FILENAME</strong>.</p>
<p><strong>Note:</strong></p>
<p><em>If <strong>options</strong> is not specified, returns all available elements.</em></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">pathinfo ($path,$options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] );</pre>
<p><strong>Example 1:</strong></p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php
$path_parts = pathinfo('/www/Technig/test.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0
?&gt;</pre>
<p>Output: returned an array.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">/www/Teching
test.inc.php
php
test.inc</pre>
<p><strong>Example 2 :</strong></p>
<p>You can print_r it directly with second argument.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true">&lt;?php
print_r(pathinfo("/Technig/test.txt",PATHINFO_BASENAME));
?&gt;</pre>
<p>It will output only basename.</p>
<pre class="theme:classic toolbar-overlay:false toolbar-hide:false toolbar-delay:false nums:false plain:false lang:php decode:true ">test.txt</pre>
<p>Although there are a lots of PHP function to talk about,  I wrap up this article here, and I hope it has been informative for you. If you want to learn more about file system in php you can refer to this article on w3schools : <a href="http://www.w3schools.com/php/php_ref_filesystem.asp" target="_blank" rel="noopener noreferrer">PHP 5 Filesystem Functions</a>.</p>
<p>The post <a href="https://www.technig.com/php-function-for-working-with-files/">5 Useful PHP Function for Working with Files</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.technig.com/php-function-for-working-with-files/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1915</post-id>	</item>
	</channel>
</rss>
