12

I want to display $row->depositdate in dd-mm-yyyy format.

If the date column in database is null the date displayed is : 01-01-1970

echo "<td align=center>".date('d-m-Y', strtotime($row->depositdate))."</td>";

If the date is null in database it should display nothing , otherwise the date in dd-mm-yyyy format should be displayed.

Thanks in advance

Sandeep

Sandy505
  • 888
  • 3
  • 15
  • 26
  • Then check if the value is null and if it is, don't expect `date` function to do your job for you - display empty string, otherwise display `date` function output. – N.B. Oct 12 '11 at 08:44

5 Answers5

21

NULL is interpreted as 0 by strtotime, since it want to be passed an integer timestamp. A timestamp of 0 means 1-1-1970.

So you'll have to check for yourself if $row->depositdate === NULL, and if so, don't call strtotime at all.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
8

NULL is converted to 0 - the epoch (1-1-1970)

Do this instead

echo "<td align=center>".($row->depositdate ? date('d-m-Y', strtotime($row->depositdate)) : '')."</td>";
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0
I am able to insert value into DB (SQL Server -- Microsoft) with PHP and value stored is: 2021-03-25 22:45:00

When I trying to fetch the value from DB:

 $db = trim($row_table['db_timestamp']);
 $timestamp = strtotime('m-d-y',$db);
 echo date("m-d-Y H:i:s", $timestamp);

output is : 01-01-1970 05:30:00
0

You need to check if $row->depositdata is_null previously or check for 0 after strtotime if the value of $row->depositdata is unrecognizable for strtotime.

  echo "<td align=center>";
  if (!is_null($row->depositdate))
  {
     $jUnixDate = strtotime($row->depositdate));
     if ($jUnixDate > 0)
     {
          echo date('d-m-Y', $jUnixDate);
     }
  }
  echo "</td>";

strtotime expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

more about unixtime and Y2K38 problem: http://en.wikipedia.org/wiki/Year_2038_problem

corretge
  • 1,751
  • 11
  • 24
-2

Oh! I know why this happens? Simply you have not included "depositdate" in your SELECT query. Firstly Change SQL query to select all with wild card sign as shown here

$sql = "SELECT * FROM `yourtable`";
  • How do you know I have not included "depositdate" in the SELECT query ? Please read the question carefully ..... – Sandy505 Apr 03 '16 at 20:32