-1

I really tried everything, I get my date from date picker like this : 05/10/2016

then I replaced the / with - . used strtotime but i always get 01-1-1970.

this is my code:

 if (isset($_POST['CampaignName']) && isset($_POST['CampaginBudget']) && isset($_POST['start_date']) && isset($_POST['end_date']) && $email  )
 {
   // receiving the post params
    $CampaignName= $_POST['CampaignName'];
    $CampaignBudget = $_POST['CampaginBudget'];
    $start_date= str_replace('/', '-', $_POST['start_date']);
    $end_date = explode('/', $end_date);
    $start_date = date('Y-m-d', strtotime($start_date));
    $end_date = date('Y-m-d', strtotime(implode('-', strtotime($end_date))));
    //$end_date = date('Y-m-d', strtotime($end_date2));
    echo $start_date;  echo $end_date;

edit : startdate works fine but end date has problem. even when they are exactly the same

html code:

Campaign Duration :<input type="text" name="start_date" class="form-control" id="datetimepicker" placeholder="mm/dd/yyyy"/  required> <span> TO </span>  <input type="text" name="end_date" class="form-control" id="datetimepicker2" placeholder="mm/dd/yyyy"/   required>
Neshat
  • 167
  • 1
  • 3
  • 15
  • your question is rather short on code (html form), not to mention what the mysql is or the db schema. – Funk Forty Niner May 17 '16 at 12:21
  • from where `$end_date` is coming ?? `$end_date` is not declared in your code... you are directly exploding end date – Manjeet Barnala May 17 '16 at 12:24
  • `$end_date = explode('/', $_POST['end_date']);` probably will be better. but why you are doing `explode()` and `implode()` here? if it works for start_date, use same approach with `str_replace()`. – mitkosoft May 17 '16 at 12:25
  • check here http://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format may it helps you – Denis Bhojvani May 17 '16 at 12:31
  • @mitkosoft I used $_POST in my code, but it is still the same – Neshat May 17 '16 at 12:32
  • 2
    yeah as stated `$end_date` that's undefined and we don't see how it's defined for the POST array for it `$_POST['end_date']` and you have a possible typo for `CampaginBudget`, so unclear there. and again; db schema, column type etc. etc. etc. The ***less*** the good people know, the ***more time*** it takes to provide you with a solution and comments will just continue to grow until we know exactly which animal(s) were dealing with here. – Funk Forty Niner May 17 '16 at 12:33
  • *gently walking out...* – Funk Forty Niner May 17 '16 at 12:39
  • @Neshat try my solution – Karthikeyani Srijish May 17 '16 at 13:00

2 Answers2

1

Did you tried this?

$originalDate = $_POST['start_date'];
echo $newDate = date("Y-m-d", strtotime($originalDate));
0

You are doint strtotime on end date double time, thats why issue is occuring:

for end date just do:

$end_date = explode('/', $end_date);
$end_date = date('Y-m-d', strtotime(implode('-', $end_date))); // remove strtotime from implode function

Full code:

if (isset($_POST['CampaignName']) && isset($_POST['CampaginBudget']) && isset($_POST['start_date']) && isset($_POST['end_date']) && $email  )
 {
   // receiving the post params
    $CampaignName= $_POST['CampaignName'];
    $CampaignBudget = $_POST['CampaginBudget'];
    $start_date= str_replace('/', '-', $_POST['start_date']);
    $start_date = date('Y-m-d', strtotime($start_date));
    $end_date = $_POST['end_date']; // add this line
    $end_date = explode('/', $end_date);
    $end_date = date('Y-m-d', strtotime(implode('-', $end_date))); // remove strtotime from implode function
    echo $start_date;  echo $end_date;
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27