开发者

Making a url with timestamp, whitespace between date and time breaks url

开发者 https://www.devze.com 2022-12-19 11:41 出处:网络
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 retur开发者_运维百科ned

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 retur开发者_运维百科ned 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)?


use urlencode :]

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消