Printing dates as strings in SQL - php

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

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'); ?>

Word of Warning: If you're not telling your program exactly how to parse a date, it's guessing.
At some point you're going to feed a date like 2022-03-04 into it and get a result that you're not expecting.
$date_str = '2022-03-03';
$dt = DateTime::createFromFormat('Y-m-d', $date_str)->format('d F Y');
var_dump($dt);
Output:
string(13) "03 March 2022"

As seen in the accepted answer to this similar post:
$originalDate = $record['fromDate']; //or $record['toDate']
$newDate = date("j F Y", strtotime($originalDate));
And use $newDate in your echo statement.
j is the day of the month (without leading zeros), F is the month name, and Y is the year.
date() php

Related

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

How to Display date as day/month/year

I want to display my date as day/month/year.
example 31st October 2011
echo '<h4>Date Of Birth: '.$row['dob'].'</br></h4>';
You can try below code ::-
echo '<h4>Date Of Birth: '.date('dS F Y', strtotime($row['dob'])).'</br></h4>';
// ^^^^^^^ Here the is format of date, in which format you want.
Output will be ::-
31st October 2011
For more you can refer click here
<?php
$mydate = "2010-03-3";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
/*Out Put*/
03rd March 2010
So if you have that data stored as or you can convert it to 3 variables representing DD MM YYYY then I'd suggest creating array of months and an array of post-fixes(don't know how to call it, but in your example it's 31st) and getting DD % 10 to use reminder for post-fix array
$month = ['January','February',....,'December']; //remember it's $month[MM-1];
$post = ['th','st','nd',...,'th'] //not sure if this on is correct but hope you get the idea
echo date('dS F Y', strtotime($row['dob']));
Refer to DateTime. I normally avoid strtotime() conversions.
Heres a working solution:
<?php
$date = '2016-11-12';
$newDate = new DateTime($date);
echo $newDate->format('jS F Y');
?>
OUTPUT:
12th November 2016

Display todays day of the week, month, day, year in THIS format in PHP?

I would like to display this format of the current date using PHP. I have googled and found a few variances of what I want to do but the company I work for wants it specifically in this format to match what they've mailed out.
Monday, March 16, 2015
Thanks.
If you looked at the PHP documentation for date you'd be able to figure it out very easily:
echo date('l, F j, Y');
which outputs
Monday, March 16, 2015
As mentioned by #alfallouji, you should have a look at the php documentation.
Here is what you need:
$now = new \DateTime();
echo $now->format('l, F j, Y');
The date format string should be "l, F d, Y".
You can display current date in this format as below:
<?php
echo date("l, F d, Y");
?>

PHP date wrong textual day, but date correct

This is weird, i'm trying to echo out a date like Monday, January 1, 2013, but its echoing out the wrong textual day. I don't have a clue why?
I have:
<?php echo date('l, F n, Y', strtotime($do['dueDate'])); ?>
And $do['dueDate'] is the date from the database of "2013-03-22". Its formatted as DATE in mysql.
When the above echos out it says: Friday, March 3, 2013
But march 3, 2013 is a sunday...
Try like this
echo date('l ,F j ,Y', strtotime($do['dueDate']));
Try this :
n --> Numeric representation of a month, without leading zeros --> 1 through 12
j --> Day of the month without leading zeros --> 1 to 31
<?php
$do['dueDate'] = "2013-03-22";
echo date('l, F j, Y', strtotime($do['dueDate']));
?>
Output :
Friday, March 22, 2013
use this
<?php
echo date('l, F j, Y', strtotime("2013-03-22")); // output Friday, March 22, 2013
working example http://codepad.viper-7.com/rF6w1U
http://php.net/manual/en/function.date.php
n is the numerical representation of the month.
try this:
$date = "2013-03-22";
echo date('l, F j, Y', strtotime($date));
outputs:
Friday, March 22, 2013

How to remove repeated day from mysql datetime using php

Following php scripts
<?php
echo date('D, F d ', strtotime($event->starttime)); echo $this->timestamp(strtotime($event->starttime));
?>
display result as,
Mon, March 21 Mon at 8:00 AM
Fri, March 25 Fri at 9:00 AM
etc
Unfortunately its showing day (Here, Mon and Fri) twice. Here how to remove the second day( ie, before string at).
I would like to show output as
Fri, March 25 at 9:00 AM
Any help please...
You don't need two echoes for this, one echo is enough. You can add the 'at' text yourself, if you prepend it with a backslash.
From the PHP Date function:
<?php
echo date('D, F d \a\t H:i A', strtotime($event->starttime));
?>
echo strftime('%a, %B %d at %I:%M %p', strtotime($event->starttime));
A lowercase d passed to date() should return only the day number... check out http://php.net/manual/en/function.date.php for a list of all the valid parameters.
Try removing the space following d?

Categories