Solved

Formatting date function

Posted on 2004-03-28
4
242 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now