I’m writing an AJAX chat script and I’m integrating a system to parse the time (basically like BBCode). The problem is, when I try casting the time (UNIX timestamp) to an integer for use with date(), it always returns “0”. The code I am using to find and replace is below. I’ve also added a sample of what I’m trying to parse
$out = preg_replace("/\[time\](.*)\[\/time\]/i",date("c",(int)"$1",$out);
Sample:
<b>GtoXic</b>: [time]1342129366[/time]
Because what you are actually casting to an integer is the literal string
$1, and converting a string to an integer works in the following way:In order to get this to work, you would use
preg_replace_callback():The
(int)cast is actually unnecessary, PHP will automatically handle this when you pass the value to a function that expects an integer.