We help IT Professionals succeed at work.

incorrect date

peter_coop
peter_coop used Ask the Experts™
on
hello
i am totally confused as to why these two statements produce dates that aren't in the database?

$boxdate_out = date('d/m/Y', strtotime($row['boxout_date']));
$boxdate_in = date('d/m/Y', strtotime($row['boxin_date']));
they are both returning 30/11/1999

any help in trouble shooting this i would be very grateful. the field in mysql is date and defaults to 0000-00-00. the date should return this value because their is no date allocated. many thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
strtotime will do whatever it takes to return something... even if you are sending it 0000-00-00.
Greg AlexanderLead Developer

Commented:
if($boxdate_out == '30/11/1999'){
      $boxdate_out = '0000-00-00';
}
if($boxdate_in == '30/11/1999'){
      $boxdate_in = '0000-00-00';
}
Are you sure the row['boxin_date'] has 0000-00-00? If so, try this: (If it's not, then echo the values and put exactly what it is printing there)

$boxdate_out = ($row['boxout_date'] == "0000-00-00" ? "0000-00-00" : date('d/m/Y', strtotime($row['boxout_date']));

$boxdate_in = ($row['boxin_date'] == "0000-00-00" ? "0000-00-00" : date('d/m/Y', strtotime($row['boxin_date']));

Open in new window

galexander07, that wouldn't serve the purpose, since the date returned will not be the same in all implementations. For  example, it is returning something 30/11/-0001 in my machine. So it is better to check in the input side itself.
A slight modification, since the date is needed in d/m/Y format:

$boxdate_out = ($row['boxout_date'] == "0000-00-00" ? "00/00/0000" : date('d/m/Y', strtotime($row['boxout_date']));

$boxdate_in = ($row['boxin_date'] == "0000-00-00" ? "00/00/0000" : date('d/m/Y', strtotime($row['boxin_date']));

Open in new window

Author

Commented:
thanks for replies
@mgh_mgharish
i removed ( from the (row because it caused error and it returns 0000-00-00 and if i enter date it returns the new date. what i don not understand is why it is doing this? where is it getting this date from? all other date functions work well. thanks

$boxdate_out = $row['boxout_date'] == "0000-00-00" ? "00/00/0000" : date('d/m/Y', strtotime($row['boxout_date']));
$boxdate_in = $row['boxin_date'] == "0000-00-00" ? "00/00/0000" : date('d/m/Y', strtotime($row['boxin_date']));
In PHP and many Unix systems, date & time are stored as the number of seconds elapsed since a given date, say (1970/1/1 or 1969/12/31 or 2000/1/1 or 1999/12/31 or 1999/11/30). So, when strtotime() fails, it returns the start date.

Put date("Y-m-j", 0); and you should see the starting date

Author

Commented:
thanks for your help. cheers
When you are 100% certain that the date you use is after 1970, you might want to consider using UNIX_TIMESTAMP and using that value in the date functions.
$query = "
select
  UNIX_TIMESTAMP(boxin_date) as unixboxindate,
  UNIX_TIMESTAMP(boxout_date) as unixboxoutdate,
from yourtable
where id = 123";

//execute query, fetch row to $row

$boxdate_out = date('d/m/Y', $row['unixboxindate']);
$boxdate_in = date('d/m/Y', $row['unixboxoutdate']);

Open in new window