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

PHP date() strtotime() function

10August 2014
Sunday



Display date string with using PHP date() strtotime() function

After retrieve the original date format string, besides with using MySQL DATE_FORMAT(), we can also use PHP date() and strtotime() functions to format the date string.


Read the content or code / instruction -

PHP date() strtotime()

Use PHP date() function to format the date string...

  1. PHP date string format example:
  2. d = day
    S = Suffix
    F = Full name of Month
    Y = 4-digits numeric year

  3. date() - date('1st parameter is date string format, 2nd parameter is a variable')
  4. example: date('dS F Y', $row['date']);
    output: 01stJan 1970 //<-- but day, month and year incorrect!
  5. PHP strtotime() - format the date to current English textual datetime string

  6. example: date('dS F Y', strtotime($row['date']));
    output: 16th July 2014

date_default_timezone_set()

Warning: strtotime() [function.strtotime]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

Since PHP 5.1.0, the date/time function has been rewritten, we need to set the timezone before use the date() and strtotime() functions. Otherwise, it will generate a E_NOTICE or E_WARNING message.

date_default_timezone_set('America/New_York'); //<-- set timezone region
echo date('dS F Y', strtotime($row['date']));

Reference mysqli datetime example statement, please open the code source as below.

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 table_date 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)
    
    
date_default_timezone_set('America/New_York'); //<--set timezone region before
    
echo date('dS F Y'strtotime($value)).'<br/>'//<-- then use date and strtotime functions display date string format
        
}
        
$result->free(); // keep to free up
    
}
$DB->close(); //close database

?>

Watch next videos and relative source page(s)

tag PHP - mysql html
Programming Web Development Networking FunG project

Topic Discussion:

No Topic Discuss Now...
programming