Display date with ordinal suffix in PHP - php

I am currently displaying date in my php code like below
$date = $news_row['date'];
$newDate = date("l M d Y", strtotime($date));
its giving me output like
Monday Mar 08 2021
Instead I want output like
Monday Mar 08th 2021
I have found function
function ordinal($number) {
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number%100) <= 13))
return $number. 'th';
else
return $number. $ends[$number % 10];
}
on this answer
But I am not getting idea how I can use it with my date format. Let me know if anyone here can help me for do it.
Thanks a lot!

$newDate = date('l M jS Y', strtotime($date));
Read about more datetime formats in PHP
https://www.php.net/manual/en/datetime.format.php
l => A full textual representation of the day of the week
M=> A short textual representation of a month, three letters,
j => Day of the month without leading zeros
S => English ordinal suffix for the day of the month, 2 characters
Y => A full numeric representation of a year, 4 digits

You can specify any character that you want:
$newDate = date("l M d\t\h Y", strtotime($date));
But if you want to get 1st, 2nd, 3rd, 4th ... you should use:
$newDate = date("l M jS Y", strtotime($date));

Related

php not friendly with date format d/m/Y

I can't convert date to format d/m/Y.
date("d-m-Y", strtotime(substr($code, 22, 6)) ); return 21-10-2014
but date("d/m/Y", strtotime(substr($code, 22, 6)) ); return 21/10/2014
How to format?
I would use DateTime to create an object with your format, and then reformat it.
<?php
$date = DateTime::createFromFormat('ymd', '140521');
echo $date->format('d/m/y'); //Output: 21/05/14
https://eval.in/208227
How would you like your formated date be displayed? The d, m and y in "d-m-Y" or "d/m/Y" are responsible for what you wish to display, not - or /
Here is a few list of parameters and their output:
d : Day of the month, 2 digits with leading zeros 01-31
m : Numeric representation of a month, with leading zeros 01-12
Y : A full numeric representation of a year, 4 digits
F : A full textual representation of a month, such as January or March
l : A full textual representation of the day of the week Sunday through Saturday
If you want to display 21 October 2014, you will have to use:
date("d F Y", strtotime(substr($code, 22, 6)) );
$originalDate = "2014-10-21";
$newDate = date("d/m/Y", strtotime($originalDate));
(see strtotime and date docs on the PHP site).

PHP- strtotime not converting properly

I don't understand why strtotime is returning a date off by a few days.
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F m, Y',$exp);
echo $exp; // returns "July 07, 2015" (NOT the 19th)
What am I missing?
UPDATE: Even if I do this:
echo date('F m, Y');
It says it's August 8 when today is August 21! Why!?!?!?
Here it is: Outputs July 19, 2015. See demo. You are using two characters to represent months in your date function. F and m are textual and numeric representations of a month. So change 'm' to 'd' to represent days. date("F d, Y") e.g. January 01, 2000
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
$exp = date('F d, Y',$exp);
echo $exp;
DEMO
Try this
$expiration = '2015-07-19'; // yyyy-mm-dd
$exp = strtotime($expiration);
//$exp = date('F m, Y',$exp);
$exp = date('jS F, Y',$exp);
Output
19th July, 2015

how to convert date format in textual date format?

I have displayed a date in a php page which is in dd-mm-yyyy format like-
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('d-m-Y');
echo $date2;
whic gives output as "23-04-2013".
but i want this like "23rd April 2013". How can i do this in php?
Check out date() formatting strings to understand how DateTime::format() works.
print $bdate->format('jS F Y');
Try this code
$bdate = new DateTime($_SESSION['booking_date']);
$date2=$bdate->format('l jS F Y');
echo $date2;
Do you consider in use Javascript ?
You can use the moment.js
[year, month, day, hour, minute, second]
moment([2010, 1, 14, 15, 25, 50]); // February 14th, 3:25:50 PM
For example... but you can find all in www.momentjs.com
this will work for you
$date2=$bdate->format('jS F Y');
echo $date2;
where
j - Day of the month without leading zeros
S - English ordinal suffix for the day of the month, 2 characters
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits

Convert date to two days from a given date

I'm trying to convert a given date to a date that's two days ahead of the given date. My code is as follows:
$date = date('D, M n', strtotime('+2 days', 'Mon, Dec 31, 2012'));
That code sort of gets it correct. It echoes "Wed, Jan 1". It gets the name of the day and the month correct. But, not the date. I have also tried another route.
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M n');
That didn't work either. Any ideas?
Thanks,
Lance
n is the format flag for month month. It's saying 1 because it's in January. Use j instead:
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M j'); //Wed, Jan 2
$newdate = date("D, M n",strtotime($oldDate. ' + 2 day'));

Convert date integer to text?

Is there anyway to convert the value of idate("z") to a date format that reads the Day, Month, and Year? My code looks like this:
$date_int = idate("z");
$date_text = strtotime($date_int);
$date = date("l, F j, Y", $date_text);
For some reason, it's still echoing Thursday, January 1, 1970.
Any ideas?
idate("z") is incorrect as that will return the day of the year. It seems like you want idate("U"), but in that case just use date() without the second parameter, it will assume time(). Example:
$date = date("l, F j, Y");
That should be all you need.
idate("z") will only return the day of the year. Today being 74. strtotime will not understand 74 as a parsing format. Therefore date() fails all together.
Assuming you want the date of the CURRENT YEAR:
$dayOfYear = 80;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")); // Wednesday, March 21, 2012
$dayOfYear = 1;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")) // Monday, January 2, 2012

Categories