Crystal Grace Crystal Grace - 2 months ago 8
HTML Question

codeigniter date and time does not convert correctly in view

dates that I want to display doesn't display properly. This is my code:

$row->created_when = '2016-09-27 11:06:54'

$row->approve_when = '02:43:06 09-28-2016'

<?php if(isset($records)): foreach($records as $row): ?>
......
<?php echo $row->first_name;?> <?php echo $row->last_name;?>
....
<?php if($row->approve_when==""){ ?>
<?php echo date("M d, Y H:i A", strtotime($row->created_when)); ?>
<?php echo $row->status;?>

<?php }else{ ?>

<?php echo date("M d, Y H:i A", strtotime($row->approve_when)); ?>
<?php echo $row->status;?>

<?php } ?>


but what displays in the view:

created when : Sep 27, 2016 11:06 AM

approve when : Jan 01, 1970 02:00 AM

approve_when doesn't converts properly

Answer

$row->approve_when = '02:43:06 09-28-2016' this is wrong date format. for strtotime function it must be:

$row->approve_when = '2016-28-09 02:43:06'

Code for converting to proper date format:

$date = '02:43:06 09-28-2016';
$ar = explode(' ', $date);
$ar2 = explode('-', $ar[1]);
$properDate = $ar2[2] . '-' . $ar2[0] . '-' . $ar2[1] . ' ' . $ar[0];

echo date('Y-m-d H:i:s', strtotime($properDate));