Receives a mysql datetime format: date, time, datetime, timestamp, year. Returns formated date/time string based on PHP symbols .
By : trube
/**
str FormatMySQLDateTime (mysql time, mysql datetime column type, string format)
Receives a mysql datetime format: date, time, datetime, timestamp, year
returns formated date/time string based on PHP symbols
mysql date format: YYYY-MM-DD
mysql time format: either HH:MM or HH:MM:SS
mysql datetime format: YYYY-MM-DD HH:MM:SS
mysql timestamp format: YYYYMMDDHHMMSS
mysql year format: YYYY
l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
D - day of the week, textual, 3 letters; e.g. "Fri"
F - month, textual, long; e.g. "January"
M - month, textual, 3 letters; e.g. "Jan"
n - month without leading zeros; i.e. "1" to "12"
m - month; i.e. "01" to "12"
j - day of the month without leading zeros; i.e. "1" to "31"
d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th"
Y - year, 4 digits; e.g. "1999"
y - year, 2 digits; e.g. "99"
z - day of the year; i.e. "0" to "365"
a - "am" or "pm"
A - "AM" or "PM"
g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
h - hour, 12-hour format; i.e. "01" to "12"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
s - seconds; i.e. "00" to "59"
$asc_datetime_format['long date'] = 'l, F j, Y';
$asc_datetime_format['normal date'] = 'F j, Y';
$asc_datetime_format['medium date'] = 'M j, Y';
$asc_datetime_format['short date'] = 'M-d-y';
$asc_datetime_format['numeric date'] = 'n/j/y';
$asc_datetime_format['normal time'] = 'g:i a';
$asc_datetime_format['time with seconds'] = 'g:i:s a';
$asc_datetime_format['datetime'] = 'F j, Y g:i a';
*/
function FormatMySQLDateTime($mysql_datetime_str, $mysql_datetime_type='date', $php_str_format='F j, Y') {
if (!empty($mysql_datetime_str)) {
// DEFAULTS
$hour = 0;
$minute = 0;
$second = 0;
$month = 1;
$day = 1;
$year = 2003;
$mysql_datetime_type = strtolower($mysql_datetime_type);
switch ($mysql_datetime_type) {
case 'date':
$date_components = explode('-', $mysql_datetime_str);
if (count($date_components) == 3) {
list($year, $month, $day) = $date_components;
} else {
return false;
}
break;
case 'time':
$time_components = explode(':', $mysql_datetime_str);
if (count($time_components) == 3) {
list($hour, $minute, $second) = $time_components;
} elseif (count($time_components) == 2){
list($hour, $minute) = $time_components;
} else {
return false;
}
break;
case 'datetime':
$datetime_components = explode(' ', $mysql_datetime_str);
if (count($datetime_components) == 2) {
list($date_str, $time_str) = $datetime_components;
$date_components = explode('-', $date_str);
if (count($date_components) == 3) {
list($year, $month, $day) = $date_components;
} else {
return false;
}
$time_components = explode(':', $time_str);
if (count($time_components) == 3) {
list($hour, $minute, $second) = $time_components;
} elseif (count($time_components) == 2){
list($hour, $minute) = $time_components;
} else {
return false;
}
} else {
return false;
}
break;
case 'timestamp':
if (strlen($mysql_datetime_str) == 12) {
$year = substr($mysql_datetime_str, 0, 4);
$month = substr($mysql_datetime_str, 4, 2);
$day = substr($mysql_datetime_str, 6, 2);
$hour = substr($mysql_datetime_str, 8, 2);
$minute = substr($mysql_datetime_str, 10, 2);
$second = substr($mysql_datetime_str, 12, 2);
} else {
return false;
}
break;
case 'year':
if (strlen($mysql_datetime_str) == 4) {
$year = $mysql_datetime_str;
if (!in_array($php_str_format, array('Y','y'))) {
$php_str_format = 'Y';
}
} else {
return false;
}
break;
default:
return false;
}
return date($php_str_format, mktime($hour, $minute, $second,$month,$day,$year));
} else {
return false;
}
} // end func
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Date Time Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
This whitepaper presents the benefits of successfully introducing static analysis into your organization using IBM Rational Software Analyzer. Additionally, it identifies some common pitfalls that can hinder the effective use of static analysis tooling as well as presents 10 simple strategies designed to help you quickly realize the value of static analysis using Rational Software Analyzer. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |