change date format in codeigniter

change date format in codeigniter



I have this view


<?PHP
$no = 0;
foreach ($dt as $key => $row) {
$no++;
?>
<td style="text-align:center;"> <?=$row->TANGGAL_KADALUARSA;?> </td>



with result


21/11/2018 0:00:00



What I want is to change the format into


21 December 2018





This has been asked many times google search how to change date format codeigniter
– Mr. ED
Sep 3 at 3:07





@Mr.ED yes I've search it before and I've tried it also but none is working
– Faiz
Sep 3 at 3:13





and what did you try faiz ? you didn't show anything what you tried - because as ED already said - this was asked and answered so many times here ... e.g. stackoverflow.com/questions/3542162/…
– sintakonte
Sep 3 at 7:30





Possible duplicate of Date Time create from format
– sintakonte
Sep 3 at 8:41




5 Answers
5



Try this


$date = "21/11/2018 0:00:00";
echo date('d F Y', strtotime(str_replace('/', '-', $date)))



PHPFiddle



FYI: with this formation you can use this for saving in DB date which has / and MySQL won't throw an error with invalid date format.


/





Yes it works BUT there is a strange things. I'm using this code <?php echo date('d F Y', strtotime(str_replace('/', '-', $row->TANGGAL_KADALUARSA))); ?> and it give different result. In 28/04/2012 0:00:00 it show 28-Apr-12. AND for 10/12/2018 0:00:00 it show 10 December 2018. How to fix this and the correct one is 10 December 2018 format.
– Faiz
Sep 3 at 10:52





before call it to check date format. and then pass
– Abdulla Nilam
Sep 3 at 11:03





could you please show me how to do that? Thanks
– Faiz
Sep 3 at 11:54



you can convert that with this sample


$date=date_create("21-11-2018 0:00:00");
echo date_format($date,"d F y"); //it will echo 21 November 18



You can try this:


<?php
$str =$dt;
echo date('"d F Y"', strtotime($str));
?>





Is there any need to duplicate existing answers?
– Nico Haase
Sep 3 at 6:53



imho the best way here is the use of the DateTime::createFromFormat function, try the following


DateTime::createFromFormat


<?php
$no = 0;
foreach ($dt as $key => $row) {

$objTanggalKadaluarsa = DateTime::createFromFormat('d/m/Y H:i:s', $row->TANGGAL_KADALUARSA);
$no++;
?>
<td style="text-align:center;"> <?=$objTanggalKadaluarsa->format('d F Y');?> </td>



try this (my format data is 2018-09-03 16:12:40)


$month = array("January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
);

echo substr($row->TANGGAL_KADALUARSA,8,2).' '.$month[substr($row->TANGGAL_KADALUARSA,5,2)-1].' '.(substr($row->TANGGAL_KADALUARSA,0,4)).' | '.substr($row->TANGGAL_KADALUARSA,-8);





this is working actually. It give result 28 April 2012 | 00:00:00. But how to remove | 00:00:00 ? If I remove .' | '.substr($row->TANGGAL_KADALUARSA,-8) it give two different result, 10 December 2018 and 28-Apr-12 format. The correct one should be 10 December 2018 format
– Faiz
Sep 3 at 11:23





try this echo (substr($row->TANGGAL_KADALUARSA,0,2)).' '.$month[(substr($row->TANGGAL_KADALUARSA,3,2))].' '.substr($row->TANGGAL_KADALUARSA,6,4);
– Premika Phutta
Sep 4 at 2:39




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)