views:

216

answers:

2

Hi,

I'm working on the Head First PHP and MySql book, and I have this problem with a date/timestamp. I'm going to make an URL with a date as a GET parameter. But when the date is returned from the database (type of timestamp), there is a white space betwen the date and the time, so the URL breaks.

How can I format the date to get included in the URL (along with the rest of the parameters)?

+2  A: 

use urlencode :]

http://php.net/manual/en/function.urlencode.php

Adam Kiss
A: 

Use urlencode. That will encode all 'url breaking' characters so that they can safely passed in an URL.

Also, consider passing the date in some other format, as a regular urlencoded datetime might look a bit ugly. You can use timestamps instead. For example, compare these two URLs which have equal dates:

http://www.domain.com/index.php?date=2010-02-09%2018:06:20

http://www.domain.com/index.php?date=1265731567

Additionally, you can use a custom format, YYYYMMDDHHIISS is commonly used, which would result in URLs like this:

http://www.domain.com/index.php?date=2010020920180620

The timestamp format is the one that works natively with PHP's date functions without needing any additional parsing, so I'd suggest using that instead.

Tatu Ulmanen