top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Calculating Date sequence in PHP

+3 votes
365 views

I am trying to calculate a date sequence using $i in conjunction with " (so the represented # is "seen" by PHP)

The code results in 2 errors:

Notice: Undefined variable: i_days_ago in test.php on line 9
Notice: Undefined variable: i_days_ago in test.php on line 13

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).

posted Nov 12, 2013 by Anderson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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');
answer Nov 12, 2013 by Sonu Jindal
Or you can 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');
Similar Questions
+2 votes

I always hate dealing with date/time stuff in php - never get it even close until an hour or two goes by....

anyway I have this:

// get two timestamp values
$exp_time = $_COOKIE[$applid."expire"];
$curr_time = time();
// get the difference
$diff = $exp_time - $curr_time;
// produce a display time of the diff
$time_left = date("h:i:s",$diff);

Currently the results are:

exp_time is 06:55:07
curr_time is 06:12:03
the diff is 2584
All of these are correct.

BUT time_left is 07:43:04 when it should be only "00:43:04". So - where is the hour value of '07' coming from?? And how do I get this right?

+2 votes

I have a date field on an html form that users may leave blank. If they do leave it blank I want to write the date 01/01/1901 into the mysql table. How can I accomplish this and where in my .php script file should I put the code?

+3 votes

How to print current date and time in C/C++, sample program will help?

+1 vote

I am working on a script to rename files after a formula (discussed here: http://www.drbunsen.org/naming-and-searching-files-part-1/ ) . The formula calls for an ID string that is constructed from the date and time the file is created, of the format YYYMMDD_HHMMSS. The closest approximation of the file creation date that I can find thus far is the inode change time returned by the stat() function. I was under the impression that this would not be too far off from the first save date, but from what I later read and my experimentation, it seems VERY far off the vast majority of the time. The inode change time is more often than not exactly the same as the last modification time, and even when it's not it's nowhere close to the actual date/time of the first time the file was saved.

The ID string here serves two purposes: to make a unique identifier, and to put the files in order by creation date and time. At the very least I figure using the inode change time should produce unique identifiers, but I would like to have chronological order if possible. Unfortunately, I am starting to believe that this is not possible. I have files going back to the mid 1990's, coming from various versions of Windows, as well as files produced over the last few years natively on the Mac. Even the creation dates/times of the native Mac files, as listed in the finder, seem radically incorrect.

Does anyone know if this is possible? Or should I just accept the fact that all dates before 8/2013 are suspect?

...