date and time question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Westy1717
Forum Newbie
Posts: 2
Joined: Mon Feb 01, 2010 4:32 pm

date and time question

Post by Westy1717 »

Very new to php so this might be a dumb question.

When a user selects a date and time on a previous webpage, the dateTimePurchased is held.

I am outputting it in the following statements:

echo "<tr><td>Date: ".$_SESSION['dateTimePurchased']."</td></tr>";

echo "<tr><td>Time: ".$_SESSION['dateTimePurchased']."</td></tr>";

Output for both is 2010-02-01 16:07:00

1. For the 1st line - Date:, how can I get the output to say Tuesday February 2, 2010
2. For the second line 4:07pm.

Can I mod just the lines I have to get the output correct?

Really appreciate any help.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: date and time question

Post by Luke »

PHP has a really convenient function called "strtotime" that takes just about any format of date and converts it to a unix timestamp (the date/time format PHP is most able to work with). Once you have the unix timestamp, you can use the "date" function to format it any way you like:

Code: Select all

$ts = strtotime($_SESSION['dateTimePurchased']);
$date = date('l F j, Y', $ts); // will be Tuesday February 2, 2010
$time = date('g:ia', $ts); // will be 4:07pm
Westy1717
Forum Newbie
Posts: 2
Joined: Mon Feb 01, 2010 4:32 pm

Re: date and time question

Post by Westy1717 »

Thank you!
Post Reply