Solved

Formatting date function

Posted on 2004-03-28
4
246 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:
Reapz earned 100 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:Reapz
ID: 10700377
And once again venkateshwarr has posted while I've been typing... LOL!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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.

738 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