Date format Issue

Post November 9th, 2006, 1:21 am

I'm trying to format date and time using PHP to a more human friendly date/time in an email.

I have a form, which is submitted, and date is input as YYYY-MM-DD. Time is a separate select box input in format HH:MM:SS

I then use PHP to combine these two boxes into a variable. Here's an example -- not the actual code, so as not to clutter the page.

Code: [ Download ] [ Select ]

<?php //I've eliminated all of this script except what's necessary.

$startdate = escape_data($_POST['start_date']); //In format YYYY-MM-DD
$starttime = escape_data($_POST['start_time']); //In format HH:MM:SS
$startjoin = "$startdate $starttime"; //To database. Now formated as YYYY-MM-DD HH:MM:SS
//Here's where the problems start.
//1969 for year, no matter what - the date is wrong too and extra numbers are added.
$startdateformat = date("l, F, j Y",$startjoin);
$starttimeformat = date("g:i A",$startjoin); //Same issue as above.
$start = "$startdateformat, $starttimeformat"; //Same issue, just joined together.

//Then I send an email later in the script. If I don't add formatting it displays as YYYY-MM-DD HH:MM:SS

$body = "You will be starting on {$start}."

//Code continues.

?>
  1. <?php //I've eliminated all of this script except what's necessary.
  2. $startdate = escape_data($_POST['start_date']); //In format YYYY-MM-DD
  3. $starttime = escape_data($_POST['start_time']); //In format HH:MM:SS
  4. $startjoin = "$startdate $starttime"; //To database. Now formated as YYYY-MM-DD HH:MM:SS
  5. //Here's where the problems start.
  6. //1969 for year, no matter what - the date is wrong too and extra numbers are added.
  7. $startdateformat = date("l, F, j Y",$startjoin);
  8. $starttimeformat = date("g:i A",$startjoin); //Same issue as above.
  9. $start = "$startdateformat, $starttimeformat"; //Same issue, just joined together.
  10. //Then I send an email later in the script. If I don't add formatting it displays as YYYY-MM-DD HH:MM:SS
  11. $body = "You will be starting on {$start}."
  12. //Code continues.
  13. ?>


Everything works fine, except that the date is not formatted correctly? Am I missing something?

I would like to format the date like Wednesday, November 8, 2007 and time as 2:20 AM

This is how it looks when it comes in the email:

You will be starting on Wednesday, December, 31 1969, 6:33 PM

(The actual date/time entered was December 2, 2007 9:00 PM)

Can someone help? Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post November 9th, 2006, 1:21 am

Post November 9th, 2006, 1:37 am

The PHP date functions 2nd parameter requires a Unix Timestamp, see time()...

Example: date('l, F, j Y',time());

Will show the current time, by default time() is what the 2nd parameter is. If the 2nd parameter is messed up it will either show the beginning of the unix epoc (the 1969/1970 date) or the current time.

You're inputting something totally different. If you want to convert things into timestamps, there are hundreds of ways, but:
http://php.net/mktime

Might be what you want.

I also suggest changing the string:
$startjoin = "$startdate $starttime";
to:
$startjoin = $startdate.' '.$starttime;//Processes faster and doesn't mess up...

Same goes for the other one with the comma.
Why no, no I'm not.

Post November 9th, 2006, 1:46 am

Hey Punk,

I'm going to visit the site linked in your reply and see what I can come up with. I'll definitely be back if I have probs.

Also, thanks for the suggestion.

I had never done anything with PHP and MySQL until a few nights ago. I had some extra time and read two full PHP/MySQL books and just started practicing. I always knew I'd be happier when I could do stuff with PHP and MySQL, but I never imagined it would open up this many more opportunities with functionality.

I appreciate your help -- I'm still learning and the books only go so far.

Thanks!

Post November 9th, 2006, 1:59 am

OK, new question.. so If I were using mktime, I could do it like this?

$startjoin = date("M-d-Y", mktime($start));

If so, then I need to completely reconfigure the way in which the variable $start is written.

Also, would I then need to change it back to YYYY-MM-DD HH:MM:SS to send to MySQL -- or will mysql read the timestamp correctly?

Post November 9th, 2006, 10:57 am

Sorry I've taken a bit, went to sleep...

Anyway, mktime takes each thing (second, minute, day, year) as seperate parameters/arguements. As seen on the link:
int mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )

So, if you need to split up the form input do something like this...
PHP Code: [ Download ] [ Select ]
<?php
 
list($year,$month,$day) = explode('-',$startdate,3);
 
list($hour,$minute,$second) = explode(':',$starttime,3);
 
$timestamp = mktime($hour,$minute,$second,$month,$day,$year);
 
$startdateformat = date('l, F, j Y',$timestamp);
 
$starttimeformat = date('g:i A',$timestamp);
 
?>
  1. <?php
  2.  
  3. list($year,$month,$day) = explode('-',$startdate,3);
  4.  
  5. list($hour,$minute,$second) = explode(':',$starttime,3);
  6.  
  7. $timestamp = mktime($hour,$minute,$second,$month,$day,$year);
  8.  
  9. $startdateformat = date('l, F, j Y',$timestamp);
  10.  
  11. $starttimeformat = date('g:i A',$timestamp);
  12.  
  13. ?>


Note though: Since your integars are formatted with leading zeros or if they are, they might mess up mktime() I'm not sure, but if it does use intval() to convert it.

Oh and with the MySQL, I just use timestamps and save them in unsigned int(10) in the DB. I don't use their date format because it's too confusing and like work, so I couldn't tell you.
Why no, no I'm not.

Post November 9th, 2006, 10:46 pm

Hey Punk,

Thanks for the reply. I had to get some sleep too, last night. I was getting frustrated with the date issue. Thanks for the info. I had seen the explode function in some other posts, and wondered if that woud help me.

I'm going to try that. I'll let you know how it works out.

Thanks again!

Brady

Post November 10th, 2006, 6:35 am

Hey Punk,

It works perfectly now!

One last question -- if this information is accessed by someone in a different time zone, will it change the time? I need the date/time to remain static.

Thanks for all of your help.

Post November 10th, 2006, 11:57 am

It's based off of whatever the servers time is, so no it won't change time depending on the user, that gets much more complex.
Why no, no I'm not.

Post November 10th, 2006, 12:57 pm

great! Then it's perfect! Thanks to you!

Thank you!

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 161 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.