PHP date showing '1970-01-01 ' after conversion

前端 未结 8 572
醉话见心
醉话见心 2020-12-04 16:44

I have a form in which date format is dd/mm/yyyy . For searching database , I hanverted the date format to yyyy-mm-dd . But when I echo

相关标签:
8条回答
  • 2020-12-04 17:13

    The issue is when your data is set to 000-00-00 or empty you must double-check and give the correct information and this issue will go away. I hope this helps.

    0 讨论(0)
  • 2020-12-04 17:19
    $date1 = $_REQUEST['date'];
    
    if($date1) {
        $date1 = date( 'Y-m-d', strtotime($date1));
    } else {
        $date1 = '';
    }
    

    This will display properly when there is a valid date() in $date and display nothing if not.
    Solved the issue for me.

    0 讨论(0)
  • 2020-12-04 17:22

    finally i have found a one line code to solve this problem

    date('d/m/Y', strtotime(str_replace('.', '-', $row['DMT_DATE_DOCUMENT'])));
    
    0 讨论(0)
  • 2020-12-04 17:27
    $inputDate = '07/05/-0001';
    $dateStrVal = strtotime($inputDate);
    if(empty($dateStrVal))
    {
      echo 'Given date is wrong'; 
    }
    else{
     echo 'Date is correct';
    }
    

    O/P : Given date is wrong

    0 讨论(0)
  • 2020-12-04 17:28

    January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.

    In other words, your strtotime($date1) returns 0, meaning that $date1 is passed in an unsupported format for the strtotime function.

    0 讨论(0)
  • 2020-12-04 17:30

    Another workaround:

    Convert datepicker dd/mm/yyyy to yyyy-mm-dd

    $startDate = trim($_POST['startDate']);
    $startDateArray = explode('/',$startDate);
    $mysqlStartDate = $startDateArray[2]."-".$startDateArray[1]."-".$startDateArray[0];
    $startDate = $mysqlStartDate;
    
    0 讨论(0)
提交回复
热议问题