Discussion:
Date sequence calculating
Ron Piggott
2013-11-12 19:25:35 UTC
Permalink
I am trying to calculate a date sequence using $i in conjunction with “ (so the represented # is “seen” by PHP)

The code (below) results in 2 errors:

Notice: Undefined variable: i_days_ago in /vhosts/mypainmanagementtracker.net/public/test.php on line 9
Notice: Undefined variable: i_days_ago in /vhosts/mypainmanagementtracker.net/public/test.php on line 13

<?php

$users_current_date = date('Y-m-d');
$i = 2;
while ( $i <= 10 ) {

$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime( $users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['starting_date'] = $dt->format('Y-m-d 00:00:00');

$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime( $users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['ending_date'] = $dt->format('Y-m-d 23:59:59');

++$i;
}
?>

Is there a way to do this without creating these errors and also “NOTICE” errors? The dates being calculated will be used in a database query (used to generate a report based on the activity between the starting and ending dates).

Ron


Ron Piggott


www.TheVerseOfTheDay.info
Jonathan Sundquist
2013-11-12 19:30:48 UTC
Permalink
I am trying to calculate a date sequence using $i in conjunction with “
(so the represented # is “seen” by PHP)
Notice: Undefined variable: i_days_ago in /vhosts/
mypainmanagementtracker.net/public/test.php on line 9
Notice: Undefined variable: i_days_ago in /vhosts/
mypainmanagementtracker.net/public/test.php on line 13
<?php
$users_current_date = date('Y-m-d');
$i = 2;
while ( $i <= 10 ) {
$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime(
$users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['starting_date'] = $dt->format('Y-m-d 00:00:00');
$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime(
$users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['ending_date'] = $dt->format('Y-m-d 23:59:59');
++$i;
}
?>
Is there a way to do this without creating these errors and also “NOTICE”
errors? The dates being calculated will be used in a database query (used
to generate a report based on the activity between the starting and ending
dates).
Ron
Ron Piggott
www.TheVerseOfTheDay.info
You need to concatenate the string. Right now php thinks that $i_days_ago
is a string since you have it in double quotes. You should do

$users_dates[$i . "_days_ago"]['starting_date'] = $dt->format('Y-m-d
00:00:00');
$users_dates[$i . "_days_ago"]["ending_date"] = $dt->format('Y-m-d
23:59:59');
George Wilson
2013-11-12 22:05:15 UTC
Permalink
Or you could always do:

$users_dates["{$i}_days_ago"]['starting_date'] = $dt->format('Y-m-d
00:00:00');

$users_dates["{$i}_days_ago"]['ending_date'] = $dt->format('Y-m-d
23:59:59');


On Tue, Nov 12, 2013 at 11:30 AM, Jonathan Sundquist
On Tue, Nov 12, 2013 at 1:25 PM, Ron Piggott <
I am trying to calculate a date sequence using $i in conjunction with “
(so the represented # is “seen” by PHP)
Notice: Undefined variable: i_days_ago in /vhosts/
mypainmanagementtracker.net/public/test.php on line 9
Notice: Undefined variable: i_days_ago in /vhosts/
mypainmanagementtracker.net/public/test.php on line 13
<?php
$users_current_date = date('Y-m-d');
$i = 2;
while ( $i <= 10 ) {
$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime(
$users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['starting_date'] = $dt->format('Y-m-d 00:00:00');
$dt = new \DateTime( date('Y-m-d H:i:s' , strtotime(
$users_current_date ) ) );
$dt->modify("-$i days");
$users_dates["$i_days_ago"]['ending_date'] = $dt->format('Y-m-d 23:59:59');
++$i;
}
?>
Is there a way to do this without creating these errors and also “NOTICE”
errors? The dates being calculated will be used in a database query
(used
to generate a report based on the activity between the starting and
ending
dates).
Ron
Ron Piggott
www.TheVerseOfTheDay.info
You need to concatenate the string. Right now php thinks that $i_days_ago
is a string since you have it in double quotes. You should do
$users_dates[$i . "_days_ago"]['starting_date'] = $dt->format('Y-m-d
00:00:00');
$users_dates[$i . "_days_ago"]["ending_date"] = $dt->format('Y-m-d
23:59:59');
Continue reading on narkive:
Loading...