PHP Date Formatting from MySQL Fatched Datetime [duplicate] - php

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.

Related

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

PHP Date Parse into a new format [duplicate]

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()));

PHP Date Conversion [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Format mysql datetime with php
How to convert the following:
2010-12-07 12:00:00
into:
December, 7th, 2010
echo date("F, jS, Y", strtotime("2010-12-07 12:00:00"));
http://php.net/manual/en/function.date.php
Check out the date function.
$myDate = strtotime('2010-12-07 12:00:00');
$formattedDate = date('F, jS, Y', $myDate)
Use date_format
http://www.php.net/manual/en/function.date-format.php
$date = date_create('2010-12-07 12:00:00');
echo date_format($date, 'F, jS, Y');
echo date('F, jS, Y',strtotime('2010-12-07 12:00:00'));
$d = strtotime('2010-12-07 12:00:00');
echo date('F, jS, Y', $d);

PHP Date Format Issue

I have date in following format (Y-m-d) as 2010-11-24, need to convert it to
1) 24 Nov, 2010
2) 24_Nov_2010
Help appreciated, thanks.
$oDate = new DateTime('2010-11-24');
echo $oDate->format('d M, Y') . "\n";
echo $oDate->format('d_M_Y') . "\n";
(d M, Y)
(d_M_Y)
So, something like
date("d M, Y");
Will output todays date:
24 Nov, 2010
Edit: I may have read your question incorrectly. Can you please clarify if this is what you want? Or you wanted to convert the 24-11-2010 into the above formats?
If the latter if what you were seeking, you can simply utilise the strtotime function:
date("d M, Y", strtotime("2010-11-24"));
Most simple, you can use strtotime() to convert 2010-11-24 into date, and use date() to get seperate pieces(Y,m,D) and convert them to whatever format you want.
Please refer this link:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
$d1 = "2010-11-24";
echo date("d M, Y", strtotime($d1)); // 24 Nov, 2010
echo date("d_M_Y", strtotime($d1)); // 24_Nov_2010

How to get AM/PM from a datetime in PHP [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM

Categories