Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Formatting date function

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
Wraith10
Asked:
Wraith10
  • 2
  • 2
1 Solution
 
venkateshwarrCommented:

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
 
venkateshwarrCommented:

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
 
AndyCommented:
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
 
AndyCommented:
And once again venkateshwarr has posted while I've been typing... LOL!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now