سؤال

I have a problem when i am converting a String Date datatype into a date time format. I am using this code

$birthday = date('Y-m-d', strtotime($rec['birthday']));

The problem is that when only 'Date-Month' fields are coming then it will generate by default '1970'.But i want to save this as 'Date-Month-0000'.how can we solve this problelm.

هل كانت مفيدة؟

المحلول

try this.

$rec['birthday']= "03/11/2011";
if(isset($rec['birthday']) && $rec['birthday'] != "")
{
    $dates = explode("/",$rec['birthday']);
  if(!isset($dates[2]))
     $dates[2] = "0000";
    $newdate = $dates[0]."-".$dates[1]."-".$dates[2];
echo $newdate;
}
else
{
    $newdate="";
}

Demo

NOTE: $rec['birthday'] should be in format DD/MM or DD/MM/YY

نصائح أخرى

Try this -

$date = explode('/', $rec['birthday']);

if(!isset($date[2])){
  $new_date = $date[0].'-'.$date[1].'-'.'0000';
}
else{
  $new_date = $date[0].'-'.$date[1].'-'.$date[2];
}

DEMO

try This: Date Format(dd/mm/yyyy)

$date = explode('/', $rec['birthday']);
if(sizeof($date) == 2)
{
  $new_date = $date[0].'/'.$date[1].'/'.'0000';
}

else
{
   $new_date = $rec['birthday'];
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top