<?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>Roshan Bhattarai's Blog -  A blog about PHP, Ajax, JavaScript, CSS and Web 2.0 &#187; Date-Time manipulation</title>
	<atom:link href="http://roshanbh.com.np/category/date-and-time-manipulation/feed" rel="self" type="application/rss+xml" />
	<link>http://roshanbh.com.np</link>
	<description>Useful Tutorials, Scripts , Tips, and Resources for all PHP and Ajax beginners and experts .</description>
	<lastBuildDate>Thu, 10 Jun 2010 11:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving time difference between hosting server and local timezone in PHP</title>
		<link>http://roshanbh.com.np/2008/04/time-difference-hosting-server-local-time-php.html</link>
		<comments>http://roshanbh.com.np/2008/04/time-difference-hosting-server-local-time-php.html#comments</comments>
		<pubDate>Mon, 14 Apr 2008 12:01:07 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[Date-Time manipulation]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[hosting server]]></category>
		<category><![CDATA[local time]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/04/time-difference-hosting-server-local-time-php.html</guid>
		<description><![CDATA[I think this is one of the common problem you&#8217;ll face while you&#8217;re working for the website of a particular country and web server is located in another different country. If client have the dedicated server then you won&#8217;t face this problem. But if you just own a single account of that server then you&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Ftime-difference-hosting-server-local-time-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F04%2Ftime-difference-hosting-server-local-time-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">I think this is one of the common problem you&#8217;ll face while you&#8217;re working for the website of a particular country and web server is located in another different country. If client have the dedicated server then you won&#8217;t face this problem. But if you just own a single account of that server then you&#8217;ll most probably face this problem of synchronizing the time difference of server time and local time.</p>
<p><span id="more-86"></span></p>
<p align="justify"><strong>Problem of time difference of remote server and local time </strong></p>
<p align="justify">I was working for Australian auction project in the beginning of my career and the client&#8217;s server was located in USA. I was using date() function to get the current date and time in my code.But, it didn&#8217;t work at all. You know the reason, I was taking the current time of USA and auction was closing according to timezone of USA, not according to Australian Time Standard. After that, I realized that I should get the local time according to website no matter where the server is hosted.</p>
<p align="justify"><strong>How to get the local time from remote server in PHP? </strong></p>
<p align="justify">Whenever you need to get the current local time and local date in your code then you must take the reference from gmdate() function available in PHP. This function returns the GMT date or time for you. You must know the time difference between GMT and your local time to be used in the project.And, adding that difference to GMT, will give you exact local time for you in PHP no matter where the web server is located.</p>
<p align="justify"><strong>Function to find the current local time in PHP </strong></p>
<p align="justify">Well, I&#8217;ve just written a function in PHP which will get the current local time or local date for your project, no matter where the web server related to your project is hosted.</p>
<p align="justify">First of All, we must define the hour and second difference from GMT with your current local time in configuration file of your project.</p>
<pre class="prettyprint" style="overflow: auto"> define("GMT_HR_DIFF",5);
 define("GMT_MIN_DIFF",45);</pre>
<p align="justify">As, you can see above the the time difference from GMT is 5 hour and 45 second which is Nepali Standard time(NST).<br />
Now, let&#8217;s look at the function to reture the current local NST in PHP no matter where the server is hosted.&#8221;2008-04-14&#8243; is the example of the date format returned by the function and &#8220;2008-04-14 12:15:03 is the format of the time returned by the function and these are the standard time format to be stored in the database.</p>
<pre class="prettyprint" style="overflow: auto">function getCurrentTime($flag='time')
{
   if($flag=='time')
    return date("Y-m-d H:i:s",mktime (gmdate("H")+GMT_HR_DIFF,gmdate("i")+GMT_MIN_DIFF,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
   else
    return date("Y-m-d",mktime(gmdate("H")+GMT_HR_DIFF,gmdate("i")+GMT_MIN_DIFF,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y")));
}</pre>
</p>
<p align="justify">As you can see above, I&#8217;ve taken the help gmdate() and mktime() function, which returns the UNIX timestamp for date.The hour and minute difference is added to GMT time to get the current local time or date for you. If you call the function without any parameter, then this function returns the current time and whenever you call this function with the other parameter like &#8220;getCurrentTime(&#8216;date&#8217;)&#8221;, then this function returns the current date according to the timezone set in two parameter.</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=86&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/04/time-difference-hosting-server-local-time-php.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Finding difference of days between two dates in PHP</title>
		<link>http://roshanbh.com.np/2008/03/finding-days-difference-php.html</link>
		<comments>http://roshanbh.com.np/2008/03/finding-days-difference-php.html#comments</comments>
		<pubDate>Tue, 18 Mar 2008 17:47:47 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[Date-Time manipulation]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[days difference]]></category>
		<category><![CDATA[php function]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/2008/03/finding-days-difference-php.html</guid>
		<description><![CDATA[Yesterday, my friend Roshan was was struggling to find the days difference between to two dates in PHP. Well, after a while I came up with the solution for him. The date format he&#8217;s using was &#8220;YYYY-MM-DD&#8221;, which is the standard format of date value stored in the MySql database. Function to find the difference [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Ffinding-days-difference-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2008%2F03%2Ffinding-days-difference-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p align="justify">Yesterday, my friend Roshan was was struggling to find the days difference between to two dates in PHP. Well, after a while I came up with the solution for him. The date format he&#8217;s using was &#8220;YYYY-MM-DD&#8221;, which is the standard format of date value stored in the MySql database.</p>
<p><span id="more-64"></span></p>
<h4>Function to find the difference of days between two dates </h4>
<pre class="prettyprint" style="overflow: auto">function daysDifference($endDate, $beginDate)
{

   //explode the date by "-" and storing to array
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   //gregoriantojd() Converts a Gregorian date to Julian Day Count
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;
}</pre>
<p align="justify">The above function is fairly simple, first of all the dates values in the format &#8220;YYYY-MM-DD&#8221; is separated into array by using <a href="http://www.php.net/explode" target="_blank">explode()</a> function. The first element of array contains the year, the second element contains the month and the last element contains the day value.</p>
<p align="justify">And the most import function in finding the days difference is <a href="http://de3.php.net/GregorianToJD" target="_blank">gregoriantojd()</a>, this function  converts a Gregorian date to Julian Day Count. This function takes three parameters- month, day and year.</p>
<p align="justify">And in the last line of the function, the difference between the Julian Day count of two dates are calculated and returned.</p>
<p><strong>Example :</strong></p>
<pre class="prettyprint" style="overflow: auto">&lt;?php
   echo daysDifference('2008-03-12','2008-03-09');
?&gt;</pre>
<p align="justify">What is the output of the above statement, I don&#8217;t think I need to write it down. But, I&#8217;m a nice guy and never leave the suspense, it&#8217;s 3 <img src='http://roshanbh.com.np/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  .</p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=64&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2008/03/finding-days-difference-php.html/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Date or Time Comparison in PHP</title>
		<link>http://roshanbh.com.np/2007/12/date-or-time-comparision-in-php.html</link>
		<comments>http://roshanbh.com.np/2007/12/date-or-time-comparision-in-php.html#comments</comments>
		<pubDate>Tue, 25 Dec 2007 18:16:00 +0000</pubDate>
		<dc:creator>Roshan</dc:creator>
				<category><![CDATA[Date-Time manipulation]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://roshanbh.com.np/date-or-time-comparision-in-php/</guid>
		<description><![CDATA[If you&#8217;ve to compare the difference between two dates or times values. How you&#8217;re going to accomplish it in PHP ? If you don&#8217;t know how to do it, then here is simple fuction for you to compare the date (as well as time) in PHP. For this comparison to take place, the argument supplied [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float:right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fdate-or-time-comparision-in-php.html"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Froshanbh.com.np%2F2007%2F12%2Fdate-or-time-comparision-in-php.html&amp;source=roshanbh&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p style="text-align: justify;">If you&#8217;ve to compare the difference between two dates or times values. How you&#8217;re going to accomplish it in PHP ? If you don&#8217;t know how to do it, then here is simple fuction for you to compare the date (as well as time) in PHP. For this comparison to take place, the argument supplied to this function must be in a USA and ISO date format.</p>
<p><span id="more-18"></span></p>
<h4>Function to compare Date or Time in PHP</h4>
<pre class="prettyprint">function greaterDate($start_date,$end_date)
{
  $start = strtotime($start_date);
  $end = strtotime($end_date);
  if ($start-$end &gt; 0)
    return 1;
  else
   return 0;
}</pre>
<p>so if there two date or time values stored in $date1 and $data2 variables then you can call that function in the following way</p>
<pre class="prettyprint" style="overflow:auto">$date1='2007-10-10';
$date1='2007-10-11';
if(greaterDate($date1,$date2))
   echo "First parameter is greater";
else
   echo "Second parameter is greater";</pre>
<p>well you can guess the result, it prints out <span style="color: #660000;">&#8220;Second parameter is greater&#8221;</span>and if you call the same function with these different values</p>
<pre class="prettyprint" style="overflow:auto">$date1='2007-10-10 12:15:27';
$date2='2007-10-10 11:17:37';</pre>
<p>The result will be <span style="color: #660000;">&#8220;First parameter is greater&#8221;</span></p>
<img src="http://roshanbh.com.np/?ak_action=api_record_view&id=18&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://roshanbh.com.np/2007/12/date-or-time-comparision-in-php.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 5/9 queries in 0.003 seconds using disk: basic
Object Caching 591/593 objects using disk: basic

Served from: roshanbh.com.np @ 2012-05-21 20:14:32 -->
