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! |
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. 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!
|
|
|
|
This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |