FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problems modifying date using regex
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Problems modifying date using regex [message #169962] Sat, 02 October 2010 02:23 Go to next message
Rob is currently offline  Rob
Messages: 5
Registered: October 2010
Karma: 0
Junior Member
I've (yet again) run into an issue with PHP that makes me question my
sanity.

I have a number of form letters available that include an option for
modifying the date contained within. For example, a letter may
reference a future date like "Please respond by {{DATE+10}}.", and a
past date could be "{{DATE-10}}".

I've got a regex that pulls the relevant information out of the letter
('/\{\{DATE([+\-])(\d{1,3})\}\}/i'), which tells me that I need to
eitheradd or subtract days. I'm passing \1 and \2 to a function which
appears verbatim below:

function _format_date ($op, $days) {
$date = new DateTime ();
$date -> modify ($op . $days . ' day');
return ($date -> format ('Y-m-d'));
}

Given the example showing exactly this operation at php.net (http://
us.php.net/manual/en/datetime.modify.php), one would expect it to
work. Unfortunately, it simply returns the current date.

I've verified that \1 and \2 are correctly being passed to the
function as $op and $days. I've tried casting the vars with no
difference. I've tried encasing them in a string ("$op$days day"), and
assigning the string to a separate variable. In all cases, it returns
the current date without modification and acts as though $op and $days
are null.

I've verified that simply using the string "+10 day" works as
expected, but there seems to be something special about using the
values from a regex that causes this to fail.

Please help!
Re: Problems modifying date using regex [message #169963 is a reply to message #169962] Sat, 02 October 2010 03:08 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 10/1/2010 10:23 PM, Rob wrote:
> I've (yet again) run into an issue with PHP that makes me question my
> sanity.
>
> I have a number of form letters available that include an option for
> modifying the date contained within. For example, a letter may
> reference a future date like "Please respond by {{DATE+10}}.", and a
> past date could be "{{DATE-10}}".
>
> I've got a regex that pulls the relevant information out of the letter
> ('/\{\{DATE([+\-])(\d{1,3})\}\}/i'), which tells me that I need to
> eitheradd or subtract days. I'm passing \1 and \2 to a function which
> appears verbatim below:
>
> function _format_date ($op, $days) {
> $date = new DateTime ();
> $date -> modify ($op . $days . ' day');
> return ($date -> format ('Y-m-d'));
> }
>
> Given the example showing exactly this operation at php.net (http://
> us.php.net/manual/en/datetime.modify.php), one would expect it to
> work. Unfortunately, it simply returns the current date.
>
> I've verified that \1 and \2 are correctly being passed to the
> function as $op and $days. I've tried casting the vars with no
> difference. I've tried encasing them in a string ("$op$days day"), and
> assigning the string to a separate variable. In all cases, it returns
> the current date without modification and acts as though $op and $days
> are null.
>
> I've verified that simply using the string "+10 day" works as
> expected, but there seems to be something special about using the
> values from a regex that causes this to fail.
>
> Please help!

Have you echoed $op and $days to see what they really contain?


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Problems modifying date using regex [message #169964 is a reply to message #169963] Sat, 02 October 2010 03:24 Go to previous messageGo to next message
Rob is currently offline  Rob
Messages: 5
Registered: October 2010
Karma: 0
Junior Member
> Have you echoed $op and $days to see what they really contain?

Yes, it correctly shows the values. I can also assign them to a
variable and display it correctly, eg:

$str1 = $op . $days . ' day';
$str2 = "$op$days day";
echo $str1 . $str2;

both return "+10 day". If, however, I try to use the result ($date ->
modify ($str1)), nothing happens, the date is unmodified.
Re: Problems modifying date using regex [message #169968 is a reply to message #169962] Sat, 02 October 2010 09:20 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Oct 2, 3:23 am, Rob <rbutle...@gmail.com> wrote:
> I've (yet again) run into an issue with PHP that makes me question my
> sanity.
>
> I have a number of form letters available that include an option for
> modifying the date contained within. For example, a letter may
> reference a future date like "Please respond by {{DATE+10}}.", and a
> past date could be "{{DATE-10}}".
>
> I've got a regex that pulls the relevant information out of the letter
> ('/\{\{DATE([+\-])(\d{1,3})\}\}/i'), which tells me that I need to
> eitheradd or subtract days. I'm passing \1 and \2 to a function which
> appears verbatim below:
>
> function _format_date ($op, $days) {
>         $date = new DateTime ();
>         $date -> modify ($op . $days . ' day');
>         return ($date -> format ('Y-m-d'));
>
> }
>
> Given the example showing exactly this operation at php.net (http://
> us.php.net/manual/en/datetime.modify.php), one would expect it to
> work. Unfortunately, it simply returns the current date.
>
> I've verified that \1 and \2 are correctly being passed to the
> function as $op and $days. I've tried casting the vars with no
> difference. I've tried encasing them in a string ("$op$days day"), and
> assigning the string to a separate variable. In all cases, it returns
> the current date without modification and acts as though $op and $days
> are null.
>
> I've verified that simply using the string "+10 day" works as
> expected, but there seems to be something special about using the
> values from a regex that causes this to fail.
>
> Please help!

Can you post the call as well?
Re: Problems modifying date using regex [message #169974 is a reply to message #169968] Sat, 02 October 2010 14:11 Go to previous messageGo to next message
Rob is currently offline  Rob
Messages: 5
Registered: October 2010
Karma: 0
Junior Member
<?php

function _format_date ($op, $days) {
echo $op . $days . ' day';
echo '<br />';
$str = "$op$days day";
echo $str;
echo '<br />';
return ($str);
// $date = new DateTime ();
// $date -> modify ($op . $days . ' day');
// return ($date -> format ('Y-m-d'));
}

$letter = 'Please respond by {{DATE+10}}.';

$letter_subs = array (
'/\{\{DATE([+\-])(\d{1,3})\}\}/i' => _format_date ('\1', '\2'),
);

$parsed_letter = preg_replace (array_keys ($letter_subs), array_values
($letter_subs), $letter);

echo $parsed_letter;

?>

The above (assuming it came through unmolested) returns:

\1\2 day
\1\2 day
Please respond by +10 day.

Because I pulled this out of a larger application that's echoing the
parsed letter, I always tested using return rather than echo. Seeing
the \1\2 being echoed now leads me to assume that what's being passed
to the aren't actual backreference results, but instead the literal
\1\2 strings, which explains 1) why date->modify fails, and 2) why I
saw it as returning the correct information.

So, I guess my question changes from why isn't the date conversion
working to "How can I get the actual backreference results passed to a
function?"
Re: Problems modifying date using regex [message #169975 is a reply to message #169974] Sat, 02 October 2010 15:43 Go to previous message
Rob is currently offline  Rob
Messages: 5
Registered: October 2010
Karma: 0
Junior Member
OK, spent a little time after breakfast pondering this, and was able
to come up with a solution:

<?php

function _format_date_callback ($matches) {
$date = new DateTime ();
$date -> modify ($matches[1] . $matches[2] . ' day');
return ($date -> format ('Y-m-d'));
}

$letter = 'Please respond by {{DATE+10}}.';

$date_sub = '/\{\{DATE([+\-])(\d{1,3})\}\}/i';

$parsed_letter = preg_replace_callback ($date_sub,
_format_date_callback, $letter);

echo $parsed_letter;

?>

Now the only remaining issue is to wrap it in a loop to catch all the
possibilities.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Array count for each value in turn syntax?
Next Topic: Frst foray into PHP _SELF
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 08:59:49 GMT 2024

Total time taken to generate the page: 0.02607 seconds