Great PHP Date Reference

May 20th, 2008

Just thought we’d give some trackback attention to a much deserved reference page.

This reference gives one of the best explanations for addition of and subtraction using PHP’s built-in Date Function. Also, easily compute days in the future, past, etc using the simple natural language capabilities in the strtotime function:

PHP Dates, addition, future dates, using strotime

Special thanks to the guys/girls over at Art of Web

Additional PHP Date Functions

If you’re too lazy to even look at the link above, here are some of our useful PHP Date functions:

Convert MySQL Date to PHP Date Object

It is really almost redundant to create a function for it, but this will give you the idea. Also - this will return a date in php’s native unix format - Not human-readable format, you will have to convert it to the format you would like it after running this function.
function mysqlDateToPhpDate($mysqldate){
return strtotime($mysqldate);
}

Get Current Date (mysql format)

You use this function to save the current date to a mysql database.
date("Y-m-d H:i:s");

Convert to Mysql Format

You use this function to save date to a mysql database.
function toMysqlDate($somedate){
return date("Y-m-d H:i:s",$somedate);
}

Get Current Date (human-readable format)

date('m/d/y h:i a');

Get Next Year’s Date (in mysql format)

date((date("Y")+1)."-m-d H:i:s");

Add A Year to Date (not today)

Returns in Mysql Format (YYYY-mm-dd):

function addYearToMysql($somedate){
return date((date("Y")+1)."-m-d H:i:s",$somedate);
}

Is MySQL Date Passed

Pass a mysql date into this function to find out if it has passed.
function isDatePassed($somedate){
if(strtotime($somedate) > time())return false;
else return true;
}

The following code will iterate all your checkbox elements (within the form you specify), and disable them all. Tested in ie and mozilla only.


function disableAllChecks(fmobj) {
for (var i=0;i var e = fmobj.elements[i];
if ( e.type=='checkbox' ) {
e.disabled = true;
}
}
}

If you want to check the checkboxes, change the red line to


e.checked = true;

In case anyone else is coming across this issue, for some reason, when attempting to select all options in a multi-select “<select>” box, for some reason, IE doesn’t work the first time through my iteration. Here is more:

IE Select Bug

WORKAROUND

Don’t set Multiple immediately before selecting options

Alright, I found it - for some reason, when you use javascript to set the “multiple=true”, you cannot then immediately set selected=’true’. It just doesn’t work in Internet Explorer. (Or at least we couldn’t get it to work - please let us know if you find another solution).

The Old Function

function selectAll(selector){
if(!selector)alert('Selector doesnt exist');
else{
selector.multiple=true;
for(var i=0;i
selector.options[i].selected = true;
}
return false;
}
}

The New

function selectAll(selector){
if(!selector)alert('Selector doesnt exist');
else{
/**selector.multiple=true; Call this far in advance, or have it already set to “multiple”**/
for(var i=0;i
selector.options[i].selected = true;
}
return false;
}
}

This tutorial will walk through the steps in adding a “Buy Now” link within your Google Calendar. Using this method, you can then embed your Google Calendar on your website, and display scheduled classes, or events that users can sign up for online.

Requirements:

1. Login to Google Checkout.

2. Goto “Tools” -> “Send an Invoice through Email”

3. Enter Invoice Information, Send

Enter the information below. (Pay special attention to the amount, and the ‘for’ fields. The “To”, “Dear”, and “Message” are required for Google Checkout, but won’t be used later on)

  • To:
  • Dear:
  • Message:
  • Amount:
  • for:

Click “Send Invoice” when finished.

4. Check your Email

Login and check your email for this new invoice.

5. Copy the hyperlink

Right click on the hyperlink that reads “Pay Now Through Google Checkout…” and click “Copy Shortcut” (if using Internet Explorer) or “Copy Link Location” (if using Firefox).

6. Create the Buy Now Link

You will now create the code for your Buy Now link. The final code will look something like the code below with “PASTELINKHERE” replaced with your copied link above.

<a href="PASTELINKHERE">Click to Buy Now</a>

So, your final code snippet would look like (you might have to scroll to view everything):

6. Login to Google Calendar

7. Create an event under the appropriate date & time

Click on the calendar day under which the class/event occurs. Then click “edit event details”.

8. Paste your final snippet under the “Description” field

Paste your final snippet (that we’ve compiled above; it should open with “<a href” and close with “<a>”) with the full class/event description within the description field.

9. Have a coke

Congrats! Your event will now show within your calendar and users will be able to buy it!

Further Questions?

Let us know

If you’re like us and you’ve always wanted to learn how to provide simple or clean urls, while still maintaining an easy .php or .asp setup in your main directory, you can do so easily with an .htaccess file. (For you apache guys/gals).

Special thanks to corz.org for their great .htaccess rewrite article.

So, you have:

http://somesite.com/services.php

and you’d like your visitors to type:

http://somesite.com/services

Solution

1. Paste the following in your .htaccess file:


Options +FollowSymlinks
RewriteEngine on
RewriteRule ^services services.php

Looking at the third line only:

  • Rewrite
  • ^services = what will appear after your domain name (e.g. http://somesite.com/services)
  • services.php = the actual page in your structure

And voila! Add as many lines as you like to this file. An example .htaccess file, for a small site might look like:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^services services.php
RewriteRule ^technology technology.php
RewriteRule ^contact contact.php
RewriteRule ^vendors vendor-registration.php
RewriteRule ^hospitals hospital-registration.php

More Information on .htaccess