Solved

Formatting date function

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

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
The viewer will learn how to dynamically set the form action using jQuery.

630 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