<?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>Val Petruchek on Web-Development &#187; php</title>
	<atom:link href="http://petruchek.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://petruchek.com</link>
	<description>PHP, JavaScript, MySQL, Apache, CSS</description>
	<lastBuildDate>Sun, 13 Sep 2020 18:58:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>British PHP</title>
		<link>http://petruchek.com/2013/08/british-php/</link>
		<comments>http://petruchek.com/2013/08/british-php/#comments</comments>
		<pubDate>Thu, 22 Aug 2013 20:02:21 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[comics]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=158</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div align=center><img src="http://petruchek.com/wp-content/uploads/2013/08/british-php.png" alt="British PHP" /></div>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2013/08/british-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get number of days in month with PHP</title>
		<link>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/</link>
		<comments>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 09:00:51 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=47</guid>
		<description><![CDATA[Problem: get number of days in certain month with PHP, if $m – is a month and $y – is an year. First solution, hacker-style (source): (31&#160;-&#160;(($m&#160;-&#160;1)&#160;%&#160;7&#160;%&#160;2)&#160;-&#160;((($m&#160;==&#160;2)&#160;&#60;&#60;&#160;!!($y&#160;%&#160;4))))&#160;?&#62; This solution doesn’t work properly for years divisible by 100, and non-divisible by &#8230; <a href="http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Problem: get number of days in certain month with PHP, if <strong>$m</strong> – is a month and <strong>$y</strong> – is an year.</p>
<p><strong>First solution, <em>hacker-style</em></strong> (<a href="http://ibash.org.ru/quote.php?id=12267" target="_blank">source</a>):</p>
<p><code><span style="color:#000000"><span style="color:#007700">(</span><span style="color:#0000bb">31&nbsp;</span><span style="color:#007700">-&nbsp;((</span><span style="color:#0000bb">$m&nbsp;</span><span style="color:#007700">-&nbsp;</span><span style="color:#0000bb">1</span><span style="color:#007700">)&nbsp;%&nbsp;</span><span style="color:#0000bb">7&nbsp;</span><span style="color:#007700">%&nbsp;</span><span style="color:#0000bb">2</span><span style="color:#007700">)&nbsp;-&nbsp;(((</span><span style="color:#0000bb">$m&nbsp;</span><span style="color:#007700">==&nbsp;</span><span style="color:#0000bb">2</span><span style="color:#007700">)&nbsp;&lt;&lt;&nbsp;!!(</span><span style="color:#0000bb">$y&nbsp;</span><span style="color:#007700">%&nbsp;</span><span style="color:#0000bb">4</span><span style="color:#007700">))))&nbsp;</span><span style="color:#0000bb">?&gt;</span></span></code></p>
<p>This solution doesn’t work properly for years divisible by 100, and non-divisible by 400 (years 1900 and 2100 are not leap)</p>
<p><strong>Second solution, <em>canonical</em>:</strong></p>
<p><code><span style="color:#000000"><span style="color:#0000bb">date</span><span style="color:#007700">(</span><span style="color:#dd0000">"t"</span><span style="color:#007700">,</span><span style="color:#0000bb">mktime</span><span style="color:#007700">(</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">$m</span><span style="color:#007700">,</span><span style="color:#0000bb">1</span><span style="color:#007700">,</span><span style="color:#0000bb">$y</span><span style="color:#007700">));</span></span></code></p>
<p>This solution works only for dates which can be presented as unix timestamps, therefore it’s not good (depends on PHP version and OS.)</p>
<p><strong>Third solution, <em>proper</em></strong>:</p>
<p><code><span style="color:#000000"><span style="color:#007700">function&nbsp;</span><span style="color:#0000bb;font-weight:bold">days_in_month</span><span style="color:#007700">(</span><span style="color:#0000bb">$y</span><span style="color:#007700">,</span><span style="color:#0000bb">$m</span><span style="color:#007700">)<br />
{<span style="color:#ff8000">//by&nbsp;val&nbsp;petruchek&nbsp;http://petruchek.com</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#0000bb">$d&nbsp;</span><span style="color:#007700">=&nbsp;</span><span style="color:#0000bb">31</span><span style="color:#007700">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;while(!</span><span style="color:#0000bb">checkdate</span><span style="color:#007700">(</span><span style="color:#0000bb">$m</span><span style="color:#007700">,</span><span style="color:#0000bb">$d</span><span style="color:#007700">,</span><span style="color:#0000bb">$y</span><span style="color:#007700">))&nbsp;</span><span style="color:#0000bb">$d</span><span style="color:#007700">--;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color:#0000bb">$d</span><span style="color:#007700">;<br />
}</span></span></code></p>
<p>This solution is based on built-in PHP function checkdate() which validates Gregorian date without unix timestamps.</p>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Format file size</title>
		<link>http://petruchek.com/2010/08/format-filesize/</link>
		<comments>http://petruchek.com/2010/08/format-filesize/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 12:29:51 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[filesize]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=25</guid>
		<description><![CDATA[Code snippet to output number of bytes with unknown capacity: function&#160;format_filesize($bytes)&#160; { &#160;&#160;&#160;&#160;if&#160;($bytes&#160;==&#160;0) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return&#160;'0.00&#160;B'; &#160;&#160;&#160;&#160;$s&#160;=&#160;array('B',&#160;'Kb',&#160;'MB',&#160;'GB',&#160;'TB',&#160;'PB'); &#160;&#160;&#160;&#160;$e&#160;=&#160;floor(log($bytes)/log(1024)); &#160;&#160;&#160;&#160;return&#160;sprintf('%.2f&#160;'.$s[$e],&#160;($bytes/pow(1024,&#160;floor($e)))); } I don&#8217;t know the author of this code.]]></description>
				<content:encoded><![CDATA[<p>Code snippet to output number of bytes with unknown capacity:</p>
<p><code><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">format_filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)&nbsp;<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">'0.00&nbsp;B'</span><span style="color: #007700">;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'B'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Kb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'MB'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'GB'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'TB'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PB'</span><span style="color: #007700">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">floor</span><span style="color: #007700">(</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">)/</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">1024</span><span style="color: #007700">));</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">'%.2f&nbsp;'</span><span style="color: #007700">.</span><span style="color: #0000BB">$s</span><span style="color: #007700">[</span><span style="color: #0000BB">$e</span><span style="color: #007700">],&nbsp;(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">/</span><span style="color: #0000BB">pow</span><span style="color: #007700">(</span><span style="color: #0000BB">1024</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">floor</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">))));<br />
}</span></code></p>
<p>I don&#8217;t know the author of this code.</p>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2010/08/format-filesize/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Curl — change your IP address</title>
		<link>http://petruchek.com/2010/08/curl-change-your-ip-address/</link>
		<comments>http://petruchek.com/2010/08/curl-change-your-ip-address/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 06:48:11 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=19</guid>
		<description><![CDATA[If you have multiple IP addresses and you want to make curl request using non-default IP, you can do it by setting CURLOPT_INTERFACE option: curl_setopt ($ch,&#160;CURLOPT_INTERFACE,&#160;'11.22.33.44'); This will work only if you really have that another IP. If you do &#8230; <a href="http://petruchek.com/2010/08/curl-change-your-ip-address/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>If you have multiple IP addresses and you want to make curl request using non-default IP, you can do it by setting <strong><em>CURLOPT_INTERFACE</em></strong> option: </p>
<p><code><span style="color: #0000BB">curl_setopt</span> <span style="color: #007700">(</span><span style="color: #0000BB">$ch</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">CURLOPT_INTERFACE</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'11.22.33.44'</span><span style="color: #007700">);</span></code></p>
<p>This will work only if you really have that another IP. If you do not have extra IPs, and your only IP is banned already, you need proxies. </p>
<p>If you want to forge your IP address, curl is wrong tool, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2010/08/curl-change-your-ip-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
