Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting date function

Posted on 2004-03-28
4
Medium Priority
?
249 Views
Last Modified: 2006-11-17
Ok so I have the basic time function for php:

    $cur_time = date( "m/d/Y H:i:s" );
    print( $cur_time );

But I'm trying to print it out tweaked a bit with different formatting. I want to output each component on a line by lane basis with the name of the field and the number value (all six fields being Month, Day, Year, Hours, Minutes, Seconds) And then I want to print the name of the month instead of the number (some form of selection perhaps) and whether it's AM or PM using 12 hour time instead of using the standard 24 hour time.

If the time has an odd number of minutes or an even number of minutes I want it to print out ODD or EVEN next to the time (I think this can be done with integer division or something)

0
Comment
Question by:Wraith10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10700356

print  "Month = ".date("m");
print  "Day     = ".date("d");
print  "Year    = ".date("y");
print  "Hours = ".date("H");

$mins = date("i");
if ($mins % 2 =0) print "even minutes";
else print "odd minutes";

print  "Minutes = ".$mins;
print  "Seconds  = ".date("s");

venkat.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10700366

print  "Month name = ".date("F");
print  "AM/PM= ".date("A");

Check the manual here for all other date formattings....
http://www.php.net/manual/en/function.date.php 
0
 
LVL 6

Accepted Solution

by:
Andy earned 400 total points
ID: 10700375
Using getdate() pulls out all the elements into a nice array for easy refernce the only drawback is that the hours are in 24 hour format so that a bit oif a mess around...

<?php
$now = getdate(time());
?>

<table width="300" border="0" cellspacing="3" cellpadding="3">
  <tr>
    <td width="80"><strong>Month:</strong></td>
    <td><?php echo $now['month']; ?></td>
  </tr>
  <tr>
    <td width="80"><strong>Day:</strong></td>
    <td><?php echo $now['mday']; ?> (<?php echo $now['weekday']; ?>)</td>
  </tr>
  <tr>
    <td width="80"><strong>Year:</strong></td>
    <td><?php echo $now['year']; ?></td>
  </tr>
  <tr>
    <td width="80"><strong>Hours:</strong></td>
    <td><?php if($now['hours'] == 0){ ?>12 AM<?php } elseif($now['hours'] > 12) { echo $now['hours']-12; ?> PM <?php } else { echo $now['hours']; ?> AM <?php } ?></td>
  </tr>
  <tr>
    <td width="80"><strong>Minutes:</strong></td>
    <td><?php echo $now['minutes']; if(is_int($now['minutes']/2)) { ?> EVEN <?php } else { ?> ODD<?php } ?></td>
  </tr>
  <tr>
    <td width="80"><strong>Seconds:</strong></td>
    <td><?php echo $now['seconds']; ?></td>
  </tr>
</table>
0
 
LVL 6

Expert Comment

by:Andy
ID: 10700377
And once again venkateshwarr has posted while I've been typing... LOL!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question