Jump to content

Date Formatting as 11-30-1999 instead of 00-00-0000


kevinfwb

Recommended Posts

I have a PHP page that displays yearly escalations for properties that we lease.  The PHP page shows 20 years of escalations that include the Start Date, End Date, Monthly Rent, and Annual Rent.  Some properties only have 10 or 15 years worth of escalations with the remaining dates being null.

 

When using the default MySQL, the date format it is displayed a YYYY-MM-DD. I've used the date format in PHP to show MM-DD-YYYY.  Before using the date function, null values would show up as 0000-00-00.  However, using the code below I get 11-30-1999 for null values instead of 00-00-0000.  I would prefer to have the null value displayed instead of using an IF statement to omit them...they want to see all 20years of escalations even if they are blank.

 

echo '    <td><div align="center">' . date ('m-d-Y', strtotime ($row['ts19'])) . "</div></td>\n";

 

 

Thanks

 

-Kevin

Link to comment
Share on other sites

Do this:

 

$time = strtotime ($row['ts19']);
if($time){
   echo '    <td><div align="center">' . date ('m-d-Y', $time) . "</div></td>\n";
}else{
   echo '    <td>'.$time.'</td>\n";
}

 

What's it printing in those wrong ones? It should be nothing, or 0, right?

Try changing the if to:

if($time > 0){

Link to comment
Share on other sites

I've made both changes with the same result.  I'm guessing that it is picking up the IF part of the statement and not the else.  The output is still showing 11-30-1999 for MySQL values that have 0000-00-00.  I would like those values to show up as 00-00-0000.

 

Just FYI in the else line of your code you start with ' and end with "  I changed the double to a single

 

I did an echo $time and it displayed 378709210

Link to comment
Share on other sites

There are 20 Rows of Escalations Term Start, Term End, Monthly Rent, Annual Rent

ts1, te1, mr1, ar1

ts2, te2, mr2, ar2

through 20

 

For this particular entry only escalations 1 through 15 were entered.  For the remaining 5 nothing was entered on the entry page.  0000-00-00 was not entered, it was just left blank.  I'm not sure how MySQL handles null entries.. does it just zero them out?

 

I can paste the entire code if needed.

 

-Kevin

Link to comment
Share on other sites

The problem you're having is that 00-00-0000 is not a valid date, and can not be treated as such with the date functions.

 

For instance:

echo date('m-d-Y', strtotime(NULL));

 

Is kind of ok, and should echo todays date (because strtotime is returning NULL from NULL). But...

echo date('m-d-Y', strtotime("0000-00-00"));

 

..is not ok. It will echo "11-30-1999" like you're getting.

 

 

This is the solution I'll give you:

<?php
function flipdate($date) {
if($date = "0000-00-00") return "00-00-0000";
return date('m-d-Y', strtotime($date)); 
}

 

Once you place it at the top of your page, you should be able to use it like this:

echo '    <td><div align="center">' . flipdate($row['ts19']) . "</div></td>\n";

 

And hopefully it will work how you want it to.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.