Dec 07 2008

php date add alternative to date_add

Published by admin under php

Here’s one for you Sukie…

The php function date_add ( DateTime $object , DateInterval $interval ) is an experimental function and shouldn’t really be used in a production site.  The way to get around this is to use strtotime ( string $time [, int $now ] ).

If you’re trying to add a week to today’s date you can simply do the following:
$new_date = strtotime("+1 week");

but if need to add a week to a time stamp of your own you can do:
$my_date = strtotime('05-Dec-2008');
$new_date = strtotime("+1 week", $my_date);

Note: The second parameter needs to be a valid date, when I’m getting data from a database I will usually get the date in to a dd-mmm-yyyy format as this explicitly defines the date. If it were written ‘05-12-2008′ it potentially could be interpreted as ‘12-May-2008′ which could then cause other issues to your data further down the line.

You could condense the code to be one line:
$new_date = strtotime("+1 week", strtotime('05-Dec-2008'));

For outputting the $new_date I’d use php’s date ( string $format [, int $timestamp ] ) function:
$new_date = strtotime("+1 week", strtotime('05-Dec-2008'));
echo date('d M Y', $new_date)

 

Have a look at php’s strtodate function and php’s date function for information on how to use.

  

No responses yet

Sep 09 2008

Photography at Wembury

Published by admin under Photography

Over the weekend I took some friends to Wembury as they were down visiting, I’d not been before but I’d heard it was a nice spot so gave me a great oportunity to head over there to have a look around.  Here’s a selection of the pictures I took using a Canon 400D, Sigma 17-70mm and a Gorrila-pod (a great piece of kit) where required.

No responses yet

Aug 21 2008

Ajax Loading Animated gif

Published by admin under General

I’ve been working with Ajax recently, I’ve been particularly impressed with how potentially powerful Ajax is.  Having a good knowledge of PHP and HTML myself has helped when it came to learning this amalgamation of technologies.  If you’re not aware what Ajax is, It stands for Asynchronous JavaScript And XML (eXtensible Markup Language).

A good introduction to it can be found on Harry Maugans in tutorial AJAX Made Easy, in addition to that the SAMS Teach Yourself in 10 Minutes have got a great little book on Ajax which explains it all really well from beginner to intermediate.

Back to the point in hand, I wanted an animated gif to display while I was collecting data from the server. That is precisely what I got when I came upon the Ajax Loading gif Generator. You can customise the gifs produced from the type of animation to the fore and background colours - A great little tool!

No responses yet

Jul 23 2008

Code Igniter mod_rewrite on Heart Internet

Published by admin under Code Igniter

I’ve used mod_rewrite quite a lot with Code Igniter and I’ve experienced issues getting it working to remove the index.php from the url on different servers before but this was another issue again. Here’s my resolution and some of my previous resolutions:

Here’s the contents of the .htaccess for Heart Internet to remove index.php:

RewriteEngine On
RewriteCond $1 !^(index\.php|css|img|js|robots\.txt) [NC]
RewriteRule ^(.+)$ index.php?/$1 [L]

SetEnv DEFAULT_PHP_VERSION 5

The SetEnv DEFAULT_PHP_VERSION 5 tells Heart to use PHP 5.

If you have any directories or specific files (like robots.txt) you need to access for css, JavaScript etc, they will need to be put them in the RewriteCond separated by a pipe (|).

In addition, I prefer working with short open tags for writing PHP. You need a file called php5.ini containing:

short_open_tag = On

For my XAMPP development I use the following code in my .htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|css|img|js) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]

For other servers I’ve needed the following:

Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|css|img|js|robots\.txt) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]

The subtle differences (that make a huge difference between it working and not working) that may have not been noticed are all located around /index.php?/$1.

I hope this of some use.

3 responses so far

Jul 16 2008

TinyMCE CMS WYSIWYG JavaScript Plugin

Published by admin under Tools

This is a great (and powerful) JavaScript that you can plug in to your site for CMS purposes to make HTML or BBCode easily edited - well that’s what I use it for. TinyMCE is a super customisable WYSIWYG (What You See Is What You Get) that ranges from being very basic (for my purposes) to very advanced, you can even skin it if you so desire.

It’s extremly easy to integrate in to your site and is widely used in various applications such as: Wordpress, PHP-Nuke, Joomla and Expression Engine to name a few. A full list can be found here.

Just download it from the TinyMCE download page. Copy the jscripts folder to the root of your htdocs directory.

Then put the following code in between your page tags:

Simple Editor:
<script src="../jscripts/tiny_mce/tiny_mce.js" type="text/javascript"></script> <script type="text/javascript"><!--
tinyMCE.init({
mode : "textareas",
theme : "simple"
});
// --></script>

Now for any <textarea> it will be replaced with the TinyMCE editor functions, the code:


<textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%">
<p>
<img src="media/logo.jpg" alt=" " hspace="5" vspace="5" width="250" height="48" align="right" /> TinyMCE is a platform independent web based Javascript HTML <strong>WYSIWYG</strong> editor control released as Open Source under LGPL by Moxiecode Systems AB. It has the ability to convert HTML TEXTAREA fields or other HTML elements to editor instances. TinyMCE is very easy to integrate into other Content Management Systems.</p>
<p>We recommend <a href="http://www.getfirefox.com" target="_blank">Firefox</a> and <a href="http://www.google.com" target="_blank">Google</a>
</p>
</textarea>

Would be displayed like:
TinyMCE Simple screen shot

The fully featured is extremely comprehensive and looks like:
TinyMCE Full Features Screen Shot

In my application I replaced the “../” for the JavaScript location to a global variable that returns the base path for my site rather than it being a relative path.

I also customised my copy to to only show the buttons I required and I had the button bar at the top, the great thing about this plugin is that you can get it to automatically produce BBCode which is useful if you’ve got a user facing platform.

To find out about how to customise TinyMCE check this out.

One response so far

Next »