473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

php function to format date's time

Hi,

I am retrieving results from an SQL query, one of which is a date

$arrival = $row['itin_arrival_day'];

I would like to write a PHP function that formats the date in the
following way:

If the time part of the date is of the form 2006-05-19 00:00:00 in
which the time is midnight, I would like the result to be printed as
"05/19/06 ---" but if the time is anything other than midnight, for
example, "2006-05-21 18:00:00" I would like the printed result to be
"05/21/06 6:00 PM".

How can I do this? - Dave

Feb 23 '06 #1
4 1367
la***********@zipmail.com wrote:
Hi,

I am retrieving results from an SQL query, one of which is a date

$arrival = $row['itin_arrival_day'];

I would like to write a PHP function that formats the date in the
following way:

If the time part of the date is of the form 2006-05-19 00:00:00 in
which the time is midnight, I would like the result to be printed as
"05/19/06 ---" but if the time is anything other than midnight, for
example, "2006-05-21 18:00:00" I would like the printed result to be
"05/21/06 6:00 PM".

How can I do this? - Dave

Untested and probably not the most efficient, but should work:

$arrival_array = explode( ' ', $arrival );

// format the date
$date_array = explode( '-', $arrival_array[0] );
$arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
$date_array[0] - 2000 );
unset( $date_array );

// format the time
if( $arrival_array[1] === '00:00:00' ) {
$arrival_time = '---';
}
else {
$time_array = explode( ':', $arrival_array[1] );
if( $time_array[0] > '12' ) {
$arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
}
else {
$arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
}
unset( $time_array );
}

print $arrival_date . ' ' . $arrival_time;
Feb 24 '06 #2

Juliette wrote:
la***********@zipmail.com wrote:
Hi,

I am retrieving results from an SQL query, one of which is a date

$arrival = $row['itin_arrival_day'];

I would like to write a PHP function that formats the date in the
following way:

If the time part of the date is of the form 2006-05-19 00:00:00 in
which the time is midnight, I would like the result to be printed as
"05/19/06 ---" but if the time is anything other than midnight, for
example, "2006-05-21 18:00:00" I would like the printed result to be
"05/21/06 6:00 PM".

How can I do this? - Dave

Untested and probably not the most efficient, but should work:

$arrival_array = explode( ' ', $arrival );

// format the date
$date_array = explode( '-', $arrival_array[0] );
$arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
$date_array[0] - 2000 );
unset( $date_array );

// format the time
if( $arrival_array[1] === '00:00:00' ) {
$arrival_time = '---';
}
else {
$time_array = explode( ':', $arrival_array[1] );
if( $time_array[0] > '12' ) {
$arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
}
else {
$arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
}
unset( $time_array );
}

print $arrival_date . ' ' . $arrival_time;


Alternatively, try something like this:

function foo($str) {
$t = strtotime($str);
return $t == mktime(0, 0, 0, date('m', $t), date('d', $t),
date('Y', $t)) ? date('m/d/y ---', $t) : date('m/d/y g:i', $t);
}

Feb 24 '06 #3
Thanks for this neat, concise function. In it, how would you add
"AM/PM" to the times that are not midnight?

Thanks, - Dave

ZeldorBlat wrote:
Juliette wrote:
la***********@zipmail.com wrote:
Hi,

I am retrieving results from an SQL query, one of which is a date

$arrival = $row['itin_arrival_day'];

I would like to write a PHP function that formats the date in the
following way:

If the time part of the date is of the form 2006-05-19 00:00:00 in
which the time is midnight, I would like the result to be printed as
"05/19/06 ---" but if the time is anything other than midnight, for
example, "2006-05-21 18:00:00" I would like the printed result to be
"05/21/06 6:00 PM".

How can I do this? - Dave

Untested and probably not the most efficient, but should work:

$arrival_array = explode( ' ', $arrival );

// format the date
$date_array = explode( '-', $arrival_array[0] );
$arrival_date = $date_array[1] . '/' . $date_array[2] . '/' . (
$date_array[0] - 2000 );
unset( $date_array );

// format the time
if( $arrival_array[1] === '00:00:00' ) {
$arrival_time = '---';
}
else {
$time_array = explode( ':', $arrival_array[1] );
if( $time_array[0] > '12' ) {
$arrival_time = ( $time_array[0] - 12 ) . ':' $time_array[1] . ' PM';
}
else {
$arrival_time = $time_array[0] . ':' $time_array[1] . ' AM';
}
unset( $time_array );
}

print $arrival_date . ' ' . $arrival_time;


Alternatively, try something like this:

function foo($str) {
$t = strtotime($str);
return $t == mktime(0, 0, 0, date('m', $t), date('d', $t),
date('Y', $t)) ? date('m/d/y ---', $t) : date('m/d/y g:i', $t);
}


Feb 24 '06 #4

la***********@zipmail.com wrote:
Thanks for this neat, concise function. In it, how would you add
"AM/PM" to the times that are not midnight?


Read this, it'll tell you everything you ever wanted to know about
date() :

<http://www.php.net/date>

Feb 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: praba kar | last post by:
Dear All, In Php we can print RFC 2822 formatted date by date('r') with parameter r. Then it will print the below format date. "Thu, 7 Apr 2005 01:46:36 -0300". I want to print same RFC 2822...
3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
4
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
11
by: sara | last post by:
I am trying my first functions in my code. I have a set of queries that runs to create temp tables with the right data for some reports. They can run for a long time, so I want the user to know...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
17
by: pamelafluente | last post by:
Hi I have to insert dates into some Access and SQL Databases. I need to be general as the target computer might be in any country. -------- - For access I wrote the follow: Function...
5
by: snow | last post by:
Hi All, ToOADate can change a date value to a double value, but if I change the Regional and Lauguage setting in control panel, for example, change English(US) to English(Australia) , the same...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.