-4

I get the data from database, which gives me date in a format like 2014-01-06 06:13:06. I want to convert this date into the format Jan 6th 2014.

Here is my code:

<?php 
$query="SELECT Min(transaction_date) as start,Max(transaction_date) as end from transactions";
$result1= mysql_query($query) or die(mysql_error());
$ors1=mysql_fetch_array($result1);
$date= $ors1['start'];
echo date_format($date, 'g:ia \o\n l jS F Y');
?>
Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56

5 Answers5

1

Try this one

 <?php 
    $query="SELECT Min(transaction_date) as start,Max(transaction_date) as end from transactions";
    $result1= mysql_query($query) or die(mysql_error());
    $ors1=mysql_fetch_array($result1);
    $date= $ors1['start'];
    echo date('dS M Y' ,strtotime($date));
 ?>

Reference url : PHP Documentation

Mitul Shah
  • 1,556
  • 1
  • 12
  • 34
0

Use strtotime to get a timestamp from string representation of the time. Then use date to format the timestamp into any format you want.

e.g.

$formatted_date = date('g:ia \o\n l jS F Y', strtotime('2014-01-06 06:13:06 '));
Latheesan
  • 23,247
  • 32
  • 107
  • 201
0

You can also try like this:

<?php
echo 'Current time: ' . date('Y-m-d H:i:s') . "\n";

$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";

$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";

$format = 'Y-m-!d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";

$format = '!d';
$date = DateTime::createFromFormat($format, '15');
echo "Format: $format; " . $date->format('Y-m-d H:i:s') . "\n";
?>

The above example will output something similar to:

Current time: 2010-04-23 10:29:35
Format: Y-m-d; 2009-02-15 10:29:35
Format: Y-m-d H:i:s; 2009-02-15 15:16:17
Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
Format: !d; 1970-01-15 00:00:00

for more info:http://in1.php.net/manual/en/datetime.createfromformat.php

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

You can try this itself in MySQL

SELECT 
    DATE_FORMAT(start, '%M %D %Y') as start_formatted,
    DATE_FORMAT(end, '%M %D %Y') as end_formatted
FROM
    (SELECT 
        Min(transaction_date) as start, Max(transaction_date) as end
    from
        transactions) t;

Example

mysql> SELECT DATE_FORMAT(NOW(),'%M %D %Y');
+-------------------------------+
| DATE_FORMAT(NOW(),'%M %D %Y') |
+-------------------------------+
| January 27th 2014             |
+-------------------------------+
1 row in set (0.00 sec)
Abdul Manaf
  • 4,768
  • 3
  • 27
  • 34
0

Try like this

$date= $ors1['start'];
$objdate=new DateTime($date);
echo date_format($objdate, 'g:ia \o\n l jS F Y');
Srinu M
  • 76
  • 4