Follow Me
Subscribes Hell Buddy Solution.

Google+
^
Back To Top

Feel Glad finally done the Meta Tags generator and video :) project
- type: seo html - 30 September 2014 Tue @Hell Buddy

close
Home Message Share Translate

MySql Date_Format with PHP foreach function

21June 2014
Saturday



How to convert and format DATE string from database?

To convert the date string format from database and run the code on phpMyAdmin. For example;

MySQL syntax: try this ~ SELECT NOW();
output example: 2014-07-28 05:15:53 // YYYY-MM-DD HH:MM:SS

But how about convert to this full textual date format -> 10th October 2013 ?
we need take 2 functions:


Read the content or code / instruction -

How to write date string format statement?

To convert MySQL date string to full textual string format...

  1. Date string specifier example:
  2. %D = Day of Month with English Suffix
    %M = Month Name
    %Y = 4 digits numeric YEAR
    %r = AM/PM

  3. MySQL DATE_FORMAT() syntax:

  4. SELECT DATE_FORMAT(1st parameter: variable, 2nd parameter: the format string);

    MySQL: try this ~ SELECT DATE_FORMAT(NOW(), '%D %M %Y %r');
    output: 28th July 2014 05:36:07 AM
  5. Using PHP foreach() function to display the format date

  6. foreach($row as $key_date => $value_date) echo $value_date;

    output: 28th July 2014 05:36:07 AM

open the content >>

see the code OR slideshow instruction


//MySQLI DATE_FORMAT with PHP foreach() function
<?php 

//connect database
$DB = new mysqli('localhost''username''password''database'); 

//Using MySQL Date_Format()
$query_date 'SELECT DATE_FORMAT(table_date, '%%%%r') FROM table_name';
$result $DB->query($query_date);
if(
$found $result->num_rows){

//Use PHP foreach() function retrieve data
while($row $result->fetch_assoc()){
    
    foreach(
$row as $key => $value)
    
    echo 
$value.'<br/>'// display format date
        
}
        
$result->free(); // keep to free up
    
}
$DB->close(); //close database

?>

Watch next videos and relative source page(s)

tag MySQL - php
Programming Web Development Networking FunG project

Topic Discussion:

No Topic Discuss Now...
networking - computer & software