Convert 2017-03-01 to March 1st 2017 format inPHP - php

How to Convert 2017-03-01 to March 1st 2017 format in PHP.
$mark_entry_last_date=$exam_dates['last_date'];
echo date('M j<\sup>S</\sup> Y', $mark_entry_last_date);
Above code outputs current date only but I need the date value for $mark_entry_last_date. How to get a result like March 1st 2017 for given date using escape characters. ?

date function expects second parameter to be timestamp (integer) not string, so you need to use strtotime:
echo date('M j<\s\up>S</\s\up> Y', strtotime($mark_entry_last_date));
Also you need to escape u for sup tag. Since u is format for displaying microseconds.

Try without tags and use strtotime()
echo date("F jS Y", strtotime("2017-03-01"));
// output March 1st 2017
For more http://php.net/manual/en/function.strtotime.php

Related

How to convert date to string DD MM YYY

I need to convert the date from 2020-11-01 to string 01 Nov 2020
So far I could achieved like this
$test = DateTime::createFromFormat('Y-m-d', '2020-11-01')->format('d m Y');
But the result is show 01 11 2020
How to make the month become Nov?
date('d M Y',strtotime('2020-11-01'));
you can use strtotime to get the time stamp and the use that time stamp to change format with date funtion
$timeStamp=strtotime("2020-11-01");
$res=date("d M Y",$timeStamp);

convert date in php day/month name, year

I have a query that inserts date in this format
$time = date("m-d-y");
But when I Fetch and output date it shows wrong date. real: October 2020 but outputs: January 1970
$time = $row1['time'];
$newDate = date('F Y', strtotime($time));
echo $newDate;
How do I output date in this format: 20 OCT, 2020
Try not to store date/time using varchar datatype. If you can change it, please do. However, if you can't change database structure, you can use date_create_from_format() to create a DateTime object from a custom format:
echo date_create_from_format('m-d-y', "10-29-20")->format('F Y');
Output:
October 2020
Edit: Change the format part to ->format('d M, Y') to match your desired format
Output:
29 Oct, 2020

How to display month (as string) and year (in 4 digits) from particular date from table?

I want to display my date as "Jan 2017" and "Jun 2017" from the dates 01-01-2017 and "01-06-2017"
You can use strtotime() and date() function for this.
Example:
echo Date('M Y', strtotime("01-01-2017"));
echo "\n";
echo Date('M Y', strtotime("01-06-2017"));
Output:
Jan 2017
Jun 2017
You should use date_format() function, to directly fetch dates in required format. For your required format, you can use the following query:
select date_format(now(),'%b-%y');
Replace now() with your column name
Refer this link for more details and formatting instructions

PHP How To Get Values From Dates

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

strtotime has issues because of date()

i have a problem with news script.
the code below will generate today's date, i think it's because that i have date() in the beggining, or maybe the line is just not written right.
the $postdate in the () is a numeric date, such as 13.11.01, again, i get the same date of today on all of the posts in the loop.
what's wrong with my line?
i need it to output the date as text one. such as December 13th, 2013
thanks
<?
$postdate = date('F jS, Y', strtotime($postdate));
?>
The problem is likely that strtotime() doesn't recognize the . character as a valid separator. See below:
Strtotime() doesn't work with dd/mm/YYYY format
First you have a typo on your question.
13.11.01 is November 1st, 2013 , Not November 13th, 2013
So, go the OOPway like this.
<?php
$dt='13.11.01';
$date = DateTime::createFromFormat('y.m.d', $dt);
echo $datex= $date->format('F jS, Y'); //"prints" November 1st, 2013

Categories