Format a string to a date

Hi guys

How can I turn a string “7th May 2020” into a date dd/mm/yyyy ?


The datetime format?

Another function to look at is strtotime().

Where are these values coming from? Are they even present on the server, i.e. your similar question in the javascrpt forum? Why do you want to convert them to that specific format, which is not exactly computer friendly? What are going to use the result for?

Theres a variable which ccontains the value “7th May 2020” being passed into the page. On this page theres a lot of javascript which does some date calculations in jquery. But my date calculations are expecting a date in dd/mm/yyyy format.

A bt messy I know - having php and jquery - hence why I posted in both php and jquiry forums. I suppose if I used jquery it would perhaps make it a little neater. But Id be happy with either.

Ive just tried the below:

$date = strtotime('7th May 2020');
	$day = date("d", strtotime($date));
	$month= date("m", strtotime($date));
	$year= date("Y", strtotime($date));
	echo $day . $month . $year;

But it returns 01011970 :unamused:

Ah! Ive just got it working by doing:

$date = strtotime('7th May 2020');
	$date = date("d/m/Y", $date);
	echo $date;
1 Like

Glad you got the PHP working.

Just a thought - I wonder if whatever creates the variable could instead create it in the format you wish? How is the variable being created?

Yes, youre probably right. I could rewrite that bit of the code to be more efficient. However, its working, and for now thats all that matters! :laughing:

1 Like

If it ain’t broke… :grinning:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.