How to get AM / PM from datetime in PHP

I have a date and time in a variable. My format is 08/04/2010 22:15:00 I want to display this as 10.15 PM . How to do it in PHP?

+63
date php formatting
Aug 04 '10 at 10:40
source share
13 answers

You need to convert it to a UNIX timestamp (using strtotime ), and then return to the required format using date .

For example:

 $currentDateTime = '08/04/2010 22:15:00'; $newDateTime = date('h:i A', strtotime($currentDateTime)); 
+133
Aug 04 '10 at 10:44
source share
 $dateString = '08/04/2010 22:15:00'; $dateObject = new DateTime($dateString); echo $dateObject->format('h:i A'); 
+21
Aug 04 '10 at 10:45
source share

Use strtotime() to make the date a UNIX mark.

For output, check the various date () parameters.

 $timestamp = strtotime("08/04/2010 22:15:00"); date("hi A", $timestamp); 
+19
Aug 04 2018-10-10 at
source share
 <?php $dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata')); echo $dateTime->format("d/m/y H:i A"); ?> 

You can use this to display a date like this

 22/06/15 10:46 AM 
+9
Jun 22 '15 at 5:19
source share

Like this:

 $date = '08/04/2010 22:15:00'; echo date('h:i A', strtotime($date)); 

Result:

 10:15 PM 

Additional Information:

+8
Aug 04 2018-10-10 at
source share

for flexibility in different formats use:

 $dt = DateTime::createFromFormat('m/d/YH:i:s', '08/04/2010 22:15:00'); echo $dt->format('g:i A') 

Check out the php manual for additional format options.

+7
Nov 27
source share

PHP code:

 date_default_timezone_set('Asia/Kolkata'); $currentDateTime=date('m/d/YH:i:s'); $newDateTime = date('h:i A', strtotime($currentDateTime)); echo $newDateTime; 

Exit: 05:03 PM

+5
Jul 16 '13 at 11:41
source share
 $currentDateTime = $row['date']; echo $newDateTime = date('l jS \of FY h:i:s A', strtotime($currentDateTime)); 
+2
Nov 28 '17 at 15:27
source share

For (PHP> = 5.2.0):

You can use the DateTime class. However, you may need to change the date format. I have not tried it. The following date format will be defined: 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, use . as a separator for 10.15 not recommended, as your users may be confused, or it is a decimal number or time format. The most common way is to use 10:15 PM

+1
Nov 18 '12 at 10:29
source share

The perfect answer for a real-time AM / PM solution

 <?php echo date('h:i A', time())?> 
+1
Jan 12 '18 at 12:09 on
source share

This is pretty easy. Assuming you have a (dateposted) field with the type โ€œtimestampโ€ in your database table already requested, and you want to display it, form it and also have AM / PM , all you need to do is as shown below.

  <?php echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted'])); ?> 

Note. The EXIT should look something like this: depending on the published date

May 21, 2014 03:05:27

0
May 21 '14 at 15:06
source share
 $time = strtotime($triprow['trip_start_date']); $myFormatForView = date("Mdy g:i A", $time); $time1 = strtotime($triprow['trip_end_date']); $myFormatForView1 = date("Mdy g:i A", $time1); echo $myFormatForView; echo $myFormatForView1; 
0
Dec 18 '17 at 10:14
source share

Just right

 {{ date('h:i A', strtotime($varname->created_at))}} 
0
Jul 19 '19 at 17:13
source share



All Articles