PHP Date Parse into a new format [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a function that prints out:
echo $post->EE_Event->primary_datetime()->start_date_and_time();
result:
21-02-15 08:00
But i want to display it as:
08:00 21st February, 2015
I need help with a function to grab the existing printed out date and reformat it.
I have tried a few snippets online, but nothing that worked.

Try this code:
$yourDate = '21-02-15 08:00'; // $post->EE_Event->primary_datetime()->start_date_and_time()
$date = DateTime::createFromFormat('j-m-y G:i', $yourDate);
echo $date->format('G:i dS F, Y');
Try it online:
http://ideone.com/a7EEK4
In PHP >= 5.3 we have excellent date API.
See more here:
http://php.net/manual/en/datetime.createfromformat.php

PHP's DateTime class has exactly what you need. You want to use the createFromFormat to parse it in, then use format to print it back out.
$myDate = $post->EE_Event->primary_datetime()->start_date_and_time();
$myDateObj = DateTime::createFromFormat('d-m-y G:i', $myDate);
echo $myDateObj->format('G:i jS F, Y');

echo date("G:i dS F, Y", strtotime($post->EE_Event->primary_datetime()->start_date_and_time()));

Related

Date convert showing wrongly [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I like to convert a date format dd/mm/yyyy to another format in PHP.
My Code:
$date = '29/01/2018';
echo date('l jS F Y', strtotime($date));
When I run the above code, it's showing me some wrong date:
Thursday 1st January 1970
Am I doing anything wrong?
You could also use the DateTime objects to help you converting dates. You can "create a datetime object from an specified format" and convert it.
$date = "29/01/2018";
$dt = DateTime::createFromFormat("d/m/Y", $date);
echo $dt->format("l jS F Y");
signifies American M/D/Y formatting
<?php
$date = '01/29/2018';
echo date('l jS F Y', strtotime($date));
output
Monday 29th January 2018

php string date return wrong date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
My code:
echo "Strtotime : ";
echo date("Y-m-d",strtotime('9 March, 2015'));
echo "<br> mktime : ";
echo date("Y-m-d",mktime('9 March, 2015'));
echo "<br> Normal : ";
echo date("Y-m-d",'9 March, 2015');
I need output 2015-03-09
You can use DateTime with date_create_from_format, if the structure of the String is always the same:
$d = '9 March, 2015';
echo date_create_from_format('d F, Y', $d)->format('Y-m-d');
See a running example: https://3v4l.org/s8EHi
I strongly recommend using the DateTime class for this:
$date = DateTime::createFromFormat ("d M, Y", "9 march, 2015");
echo $date->format ("Y-m-d");
If you check the PHP manual for DateTime, you can get a whole lot of other really good tips on how to further take advantage of this class. Which makes working with dates in PHP a lot easier than with the old functions. ;)
if you want to show date - month - year you can apply this
<?php
echo date('Y-m-d');
?>
I think you should use this code to get exact date format.
$orig_date = "22/09/2016";
$date = str_replace ('/', '-', $orig_date);
echo date('Y-m-d', strtotime ($date));
See more date and time format example here

How to convert date('H:i A d-M-Y') format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date format in php.
I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.
I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.
How do I do that?
Use createFromFormat function
$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
As i Commented out both the format, You need to change the format before changing final for final format.
If you need 3 at 03, just use g at h.
General: Online Check
$date = '27-05-2016 15:30';
echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016
Or ,
using Object Oriented: Online Check
$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);
echo $date->format('h:i A d-M-Y');

PHP Date Formatting from MySQL Fatched Datetime [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Can anyone please help?
I have searched many website (including stackoverflow) but can't find my exact answer.
It may be so easy, but I need to know.
2014-11-21 18:49:55
I need to convert it to
6:49 PM, 21th November, 2014
Can anyone help me please?
Thanks in advance for helping.
This should work for you:
$date = "2014-11-21 18:49:55";
echo date('g:i A, dS F Y', strtotime($date));
Output:
6:49 PM, 21st November 2014
Try this-
$date = "2014-11-21 18:49:55";
echo date('g:i A, jS F, Y', strtotime($date));
Output -
6:49 PM, 21st November, 2014
$originalDate = "2014-11-21 18:49:55";
$newDate1 = date("g:i A,", strtotime($originalDate));
$newDate2 = date("d", strtotime($originalDate));
$newDate3 = date("F, Y", strtotime($originalDate));
echo $newDate1.' '.$newDate2.'th '.$newDate3;
<?php
$providedTime = '2014-11-21 18:49:55';
$newTime = strtotime($providedTime);
echo date('g:i A, dS F Y', $newTime);
?>
[Working example][1]
We can use PHP's date() function to get the provided date in required format.
the date() function has various parameters and we can use nice use of them.
Hope it works for you.

Formatting a date to a specific format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I have the following data available : 01/02/2014
What I need to format to is: 2014-01-02 00:00:00
What is the best way to do this in PHP?
$dt = DateTime::createFromFormat('m/d/Y', '01/02/2014');
echo $dt->format('Y-m-d 00:00:00');
See it in action
$val="01/02/2014";
echo date("Y-m-d H:i:s", strtotime($val));
Demo
First of all you should try it yourself.
you should study php date format documentation : http://www.php.net/manual/en/datetime.formats.date.php
Live demo:
https://eval.in/93149
$d = "01/02/2014";
echo date('Y-m-d H:i:s',strtotime($d));
OUTPUT:
2014-01-02 00:00:00
By using PHP date function
$date = "1st April 2000";
OR
$date = "01/04/2000";
echo date("Y-m-d H:i:s",strtotime($date));

Categories