<?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>Skidoosh &#187; PHP</title> <atom:link href="http://www.skidoosh.co.uk/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://www.skidoosh.co.uk</link> <description>Skidoosh - PHP, Python, Django, Ruby on Rails Web Developer in North Wales</description> <lastBuildDate>Sat, 12 Jun 2010 09:08:31 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Create Twitter like date formatted strings with PHP</title><link>http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/</link> <comments>http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/#comments</comments> <pubDate>Mon, 12 Apr 2010 13:50:46 +0000</pubDate> <dc:creator>Glyn Mooney</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Snippets]]></category> <category><![CDATA[Twitter]]></category> <category><![CDATA[Date]]></category> <category><![CDATA[Time]]></category> <category><![CDATA[UNIX Timestamp]]></category><guid isPermaLink="false">http://www.skidoosh.co.uk/?p=82</guid> <description><![CDATA[A small function to produce Twitter like dates from various date formats. Just feed it a date, it converts it to a time (a unix timestamp), works out the difference and returns a "nice" formatted date i.e. About 1 week ago. Hope this helps.]]></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%2Fwww.skidoosh.co.uk%2Fphp%2Fcreate-twitter-like-date-formatted-strings-with-php%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fphp%2Fcreate-twitter-like-date-formatted-strings-with-php%2F&amp;source=skidoosh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br /> </a></div><p>Well I got a little bored and decided to code this function. It creates the old Twitter like date format i.e. About 4 minutes ago, etc.. As you can see the code below has a few tests so you can decipher how to use it.</p><pre name="code" class="php">
&lt;?php
	#If your running PHP > 5.3 you need to set this or your app will throw errors
	date_default_timezone_set('Europe/London');

	function twitter_time_format ($date) {

		$blocks = array (
			array('year',  (3600 * 24 * 365)),
			array('month', (3600 * 24 * 30)),
			array('week',  (3600 * 24 * 7)),
			array('day',   (3600 * 24)),
			array('hour',  (3600)),
			array('min',   (60)),
			array('sec',   (1))
		);

		#Get the time from the function arg and the time now
		$argtime = strtotime($date);
		$nowtime = time();

		#Get the time diff in seconds
		$diff    = $nowtime - $argtime;

		#Store the results of the calculations
		$res = array ();

		#Calculate the largest unit of time
		for ($i = 0; $i < count($blocks); $i++) {
			$title = $blocks[$i][0];
			$calc  = $blocks[$i][1];
			$units = floor($diff / $calc);
			if ($units > 0) {
				$res[$title] = $units;
			}
		}

		if (isset($res['year']) &#038;&#038; $res['year'] > 0) {
			if (isset($res['month']) &#038;&#038; $res['month'] > 0 &#038;&#038; $res['month'] < 12) {
				$format      = "About %s %s %s %s ago";
				$year_label  = $res['year'] > 1 ? 'years' : 'year';
				$month_label = $res['month'] > 1 ? 'months' : 'month';
				return sprintf($format, $res['year'], $year_label, $res['month'], $month_label);
			} else {
				$format     = "About %s %s ago";
				$year_label = $res['year'] > 1 ? 'years' : 'year';
				return sprintf($format, $res['year'], $year_label);
			}
		}

		if (isset($res['month']) &#038;&#038; $res['month'] > 0) {
			if (isset($res['day']) &#038;&#038; $res['day'] > 0 &#038;&#038; $res['day'] < 31) {
				$format      = "About %s %s %s %s ago";
				$month_label = $res['month'] > 1 ? 'months' : 'month';
				$day_label   = $res['day'] > 1 ? 'days' : 'day';
				return sprintf($format, $res['month'], $month_label, $res['day'], $day_label);
			} else {
				$format      = "About %s %s ago";
				$month_label = $res['month'] > 1 ? 'months' : 'month';
				return sprintf($format, $res['month'], $month_label);
			}
		}

		if (isset($res['day']) &#038;&#038; $res['day'] > 0) {
			if ($res['day'] == 1) {
				return sprintf("Yesterday at %s", date('h:i a', $argtime));
			}
			if ($res['day'] <= 7) {
				return date("\L\a\s\\t l \a\\t h:i a", $argtime);
			}
			if ($res['day'] <= 31) {
				return date("l \a\\t h:i a", $argtime);
			}
		}

		if (isset($res['hour']) &#038;&#038; $res['hour'] > 0) {
			if ($res['hour'] > 1) {
				return sprintf("About %s hours ago", $res['hour']);
			} else {
				return "About an hour ago";
			}
		}

		if (isset($res['min']) &#038;&#038; $res['min']) {
			if ($res['min'] == 1) {
				return "About one minut ago";
			} else {
				return sprintf("About %s minuts ago", $res['min']);
			}
		}

		if (isset ($res['sec']) &#038;&#038; $res['sec'] > 0) {
			if ($res['sec'] == 1) {
				return "One second ago";
			} else {
				return sprintf("%s seconds ago", $res['sec']);
			}
		}
	}

	$now            = date(DATE_RFC2822, time());
	$anhourago      = date(DATE_RFC2822, time() - (3600));
	$fourhoursago   = date(DATE_RFC2822, time() - (3600 * 4));
	$yesterday      = date(DATE_RFC2822, time() - (3600 * 24));
	$lastweek       = date(DATE_RFC2822, time() - (3600 * 24 * 6));
	$lastmonth      = date(DATE_RFC2822, time() - (3600 * 24 * 31));
	$threemonthsago = date(DATE_RFC2822, time() - (3600 * 24 * 31 * 3));
	$ayearago       = date(DATE_RFC2822, time() - (3600 * 24 * 365));
	$fiveyearsago   = date(DATE_RFC2822, time() - (3600 * 24 * 365 * 5));

	echo 'Now (should be empty): '              . twitter_time_format($now) . '';
	echo 'An Hour Ago: '      . twitter_time_format($anhourago) . '';
	echo 'Four Hours Ago: '   . twitter_time_format($fourhoursago) . '';
	echo 'Yesterday: '        . twitter_time_format($yesterday) . '';
	echo 'Last Week: '        . twitter_time_format($lastweek) . '';
	echo 'Last Month: '       . twitter_time_format($lastmonth) . '';
	echo 'Three Months Ago: ' . twitter_time_format($threemonthsago) . '';
	echo 'A Year Ago: '       . twitter_time_format($ayearago) . '';
	echo 'Five Years Ago: '   . twitter_time_format($fiveyearsago) . '';
?&gt;
</pre><p>Hope this helps!</p> ]]></content:encoded> <wfw:commentRss>http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Convert a date to a UNIX timestamp with PHP</title><link>http://www.skidoosh.co.uk/php/covert-a-date-to-a-unix-timestamp-with-php/</link> <comments>http://www.skidoosh.co.uk/php/covert-a-date-to-a-unix-timestamp-with-php/#comments</comments> <pubDate>Mon, 15 Mar 2010 23:43:11 +0000</pubDate> <dc:creator>Glyn Mooney</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Date]]></category> <category><![CDATA[Time]]></category> <category><![CDATA[UNIX Timestamp]]></category><guid isPermaLink="false">http://www.skidoosh.co.uk/?p=74</guid> <description><![CDATA[In this article I will cover how to convert a date from such things as a database like MySQL, RSS feed or other formats into a UNIX timestamp.]]></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%2Fwww.skidoosh.co.uk%2Fphp%2Fcovert-a-date-to-a-unix-timestamp-with-php%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fphp%2Fcovert-a-date-to-a-unix-timestamp-with-php%2F&amp;source=skidoosh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br /> </a></div><p>In this article I will cover how to convert a date from such things as a database like MySQL, RSS feed or other formats into a UNIX timestamp. Firstly for some people it will be handy to know what a UNIX timestamp actually is. A UNIX timestamp is the number of seconds that have passed since midnight on the 1st January, 1970 also known as the birth of modern computing.</p><p>In PHP there two; nice, simple functions for generating UNIX timestamps. The first is the &#8220;time()&#8221; function used as follows:</p><pre name="code" class="php">
    &lt;?php
        //Get the current UNIX timestamp
        $now = time();
        //Get the date one week from now as a UNIX timestamp
        $nextweek = $now + (3600 * 24 * 7);
        //Get the date last week as a UNIX timestamp
        $nextweek = $now + (3600 * 24 * 7);
        //Compare the dates
        if ($lastweek &lt; $nextweek) echo 'Next week is greater than last week';
    ?&gt;
</pre><p>I think you can gather from that that UNIX timestamps are quite useful when you need to calculate or compare dates. Because a UNIX timestamp is in seconds it is treated like any other integer number.</p><p>Now for the function we would use to generate a UNIX timestamp from a date:</p><pre name="code" class="php">
    &lt;?php
        //The following date could be any date in
        //ISO 8601, RFC 2822 and even non ISO
        //or RFC standards
        $date = date('Y-m-d h:i:s');
        //Get the UNIX timestamp
        $timestamp = strtotime($date);
    ?&gt;
</pre><p>As you can see it&#8217;s really quite simple to get a UNIX timestamp from a date. But that&#8217;s not all you can do all sorts of amazing things with dates in PHP. To see more great examples of just how flexible the strtotime function really is just look at the PHP function reference at <a href="http://php.net/manual/en/function.strtotime.php" title="Official PHP strtotime reference">http://php.net/manual/en/function.strtotime.php</a>.</p><p>Hope this helps.</p> ]]></content:encoded> <wfw:commentRss>http://www.skidoosh.co.uk/php/covert-a-date-to-a-unix-timestamp-with-php/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>CakePHP Aptana and Eclipse PDT code completion that works</title><link>http://www.skidoosh.co.uk/php/cakephp-aptana-and-eclipse-pdt-code-completion-that-works/</link> <comments>http://www.skidoosh.co.uk/php/cakephp-aptana-and-eclipse-pdt-code-completion-that-works/#comments</comments> <pubDate>Thu, 17 Dec 2009 00:44:38 +0000</pubDate> <dc:creator>Glyn Mooney</dc:creator> <category><![CDATA[Aptana]]></category> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Eclipse]]></category> <category><![CDATA[PHP]]></category><guid isPermaLink="false">http://www.skidoosh.co.uk/?p=68</guid> <description><![CDATA[A short guide on how to enable CakePHP auto completion within the Aptana Studio or Eclipse PDT IDE for Models within controllers and helpers. This actually works!]]></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%2Fwww.skidoosh.co.uk%2Fphp%2Fcakephp-aptana-and-eclipse-pdt-code-completion-that-works%2F"><br /> <img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.skidoosh.co.uk%2Fphp%2Fcakephp-aptana-and-eclipse-pdt-code-completion-that-works%2F&amp;source=skidoosh&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br /> </a></div><p>I keep seeing people asking the question &#8220;How do I get code completion to work in Aptana and Eclipse PDT?&#8221;. A lot of the time I see the Helper fix but nothing else! With a bit of fiddling I&#8217;ve finally got a complete solution <strong>that works!</strong> Here how I did it:</p><p>Follow this guide to enable the .thtml or .ctp file extentions: <a href="http://bakery.cakephp.org/articles/view/setting-up-eclipse-to-work-with-cake" title="Setting up Eclipse to work with Cake" target="_blank">Setting up Eclipse to work with Cake</a></p><p>Create a dummy file in your cake root directory with the following code:</p><pre name="code" class="php">
&lt;?php
   exit();
   //declare and define all of the helpers
   //to helper enable codecompletion
   if(1 == 0) {
        $ajax = new AjaxHelper();
        $cache = new CacheHelper();
        $form = new FormHelper();
        $html = new HtmlHelper();
        $javascript = new JavascriptHelper();
        $number = new NumberHelper();
        $session = new SessionHelper();
        $text = new TextHelper();
        $time = new TimeHelper();
    }
?&gt;
</pre><p>Then to get the Model code completion from within controllers create an app_controller.php file in you app directory and add a dummy function with the following code:</p><pre name="code" class="php">
&lt;?php
    class AppController extends Controller {
        public function dummy () {
            //for each of your models add a definition and declaration
            //like the ones that follow
            $this->User = new User();
            $this->Post = new Post();
        }
    }
?&gt;
</pre><p>So with a bit of luck this will help you on your way. Thanks for reading!</p> ]]></content:encoded> <wfw:commentRss>http://www.skidoosh.co.uk/php/cakephp-aptana-and-eclipse-pdt-code-completion-that-works/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 7/13 queries in 0.008 seconds using disk

Served from: www.skidoosh.co.uk @ 2010-09-08 16:56:47 -->