Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing dates as strings in SQL

I’m building a CMS system using PHP for the first time. Part of this project involves me printing out a date from a column in my database using a loop. This is what the loop currently looks like:

<td><?php echo $record['fromDate']; ?> - <?php echo $record['toDate']; ?> | <?php echo $record['location']; ?></td>

The date is printing out like this: 2022-03-03 – 2022-03-23

What can I do to print it out as follows: 03 March 2022 – 23 March 2022

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To begin, this can be interpreted as PHP problem rather than SQL

You can use date_format() PHP function and format it with 'd F Y'while displaying with HTML

date_format(date_create('2000-01-01'), 'd F Y')
// 01 January 2000

As per docs

F – A full textual representation of a month, such as January or March


so your code would look like

<?php echo date_format(date_create($record['fromDate']), 'd F Y'); ?> - <?php echo date_format(date_create($record['toDate']), 'd F Y'); ?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading