PHP exporting dates in d-m-year and with an odd default date

Attached is the Reports.PHP file where our data is imported from SQL and exported to a PDF report.  Search for the following block to see where the sub title with the dates in question are being gathered.  FYI, this site was developed by Eastern European developers no longer accessible.  

$text = '';
        foreach($crits as $title => $value) {
            $text .= "<tr><td>&nbsp;&nbsp;$title:&nbsp;&nbsp;$value</td></tr>";
        }
        $html .= "<table style='width:100%; cellspacing='0'> <tr><td><b>Report Criteria</b></td></tr>$text</table><br><br>";

 An example of the output for the above is

Report Criteria
Start Date: 31-12-1969
End Date: 31-12-1969

$title is Start Date: and $value is 31-12-1969

The dates being gathered here are the start and end dates entered in by the user as search criteria.  When displayed in the PDF sub title the dates are displaying as d-m-y, rather than the desired m-d-y and if the search criteria fields are left empty by the user the dates displayed default to and are displayed as 31-12-1969, rather the desired nothing.  Is there a way to modify the way the dates are being displayed to the m-d-y format and to display nothing instead of 31-12-1969 if the users don't enter a search criteria date?

Thanks!!
reports.php
ReportScreenShot.png
woodwynAsked:
Who is Participating?
 
NorieConnect With a Mentor VBA ExpertCommented:
Isn't that part of the code setting the criteria for the query?

If it is then it's probably not where you want to deal with the default date issue.

PS Where did you 'drop' the code I suggested into the original code?

It's actually meant to replace this part of the code,
        $text = '';
        foreach($crits as $title => $value) {
            $text .= "<tr><td>&nbsp;&nbsp;$title:&nbsp;&nbsp;$value</td></tr>";
        }

Open in new window

and it should format the dates as required and deal with the 'default' date issue.
0
 
NorieConnect With a Mentor VBA ExpertCommented:
There might be a better way but you could try something like this.
$text = '';
        foreach($crits as $title => $value) {
            if ($value== "") {
                $dateformatted="";
            } else {
                $date=date_create($value);
                $dateformatted=date_format($date,"m/d/Y");
            }
            $text .= "<tr><td>&nbsp;&nbsp;$title:&nbsp;&nbsp;$dateformatted</td></tr>";
        }

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
31-12-1969 equals a timestamp of 0.  The date formatting code starts with $dStartDate.  The database is probably returning '0' which strtotime() will convert into the minimum datetime of a timestamp which is 1970-01-01 or sometimes 1969-12-31.  To not show that time, you would have to detect the '0' and not let strtotime() convert it to a date.
0
 
woodwynAuthor Commented:
Norie - Dropping this block into Reports.PHP generates an error:
Fatal error: Call to undefined function date_create() in E:\Inetpub\nParallel-tst\EOL\application\entity\reports.php on line 259

I don't know much PHP.  Any help you can offer hear would be great.
0
 
woodwynAuthor Commented:
I was able to find where the date format was being set to D-M-Y and changed that to M-D-Y.  

I have not resolved the 1969/12/31 default date issue.  I am betting it could be solved in the following block.  Any suggestions?

        $dStartDate =  isset($values['@dStartDate']) ? date('Y-m-d 00:00:01', strtotime($values['@dStartDate'])) : 'GETDATE()';
        $crits['Start Date'] =  date('m-d-Y', strtotime($dStartDate));
        $dEndDate = isset($values['@dEndDate']) ?  date('Y-m-d 23:59:59', strtotime($values['@dEndDate'])) : 'GETDATE()';
        $crits['End Date'] = date('m-d-Y', strtotime($dEndDate));
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.