hopeless_reckless Posted August 13, 2008 Share Posted August 13, 2008 Hey all, I am building a calendar and I want to break-out the calendar entries by day so there will be multiple listings by day. I have to keep the original formating at the MySQL level for the user's benefit. However, I want to do day comparisons with the formating date so I can do the groupings. Here's my code and I am not sure why it isn't converting: $event_start_converted = date('D', strtotime($event_start)); print "<br>Event Start Date " . $event_start . "<br>"; print "Event Start Date Converted " . $event_start_converted . "<br>"; Here's the output: Event Start Date Wed 8-13 10:00 AM Event Start Date Converted Thu Does anyone have an idea what is wrong? event_start is being stored in the DB as a date/time field type. Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/ Share on other sites More sharing options...
kenrbnsn Posted August 13, 2008 Share Posted August 13, 2008 What is the value of $event_start as it is in the database? It certainly isn't "Wed 8-13 10:00 AM". Ken Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615710 Share on other sites More sharing options...
hopeless_reckless Posted August 13, 2008 Author Share Posted August 13, 2008 It is stored as 2008-08-13 10:00:00 Here is the SQL query: select DATE_FORMAT(created, '%M %d, %Y') as created, title, created_by, DATE_FORMAT(event_start, '%a %c-%e %l:%i %p') as event_start, DATE_FORMAT(event_end, '%a %c-%e %l:%i %p') as event_end, description from Calendar c where event_start between curdate() and curdate()+7 order by c.event_start" Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615712 Share on other sites More sharing options...
kenrbnsn Posted August 13, 2008 Share Posted August 13, 2008 When I try this test script: <?php $event_start = 'Wed 8-13 10:00 AM'; $event_start_converted = date('D', strtotime($event_start)); print " Event Start Date " . $event_start . " "; print "Event Start Date Converted " . $event_start_converted . " "; ?> The output is Event Start Date Wed 8-13 10:00 AM Event Start Date Converted Wed Which looks correct. If you run the above script, does it work? If so, something else must be wrong with your original script. Ken Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615880 Share on other sites More sharing options...
hopeless_reckless Posted August 13, 2008 Author Share Posted August 13, 2008 Here's the context of how it works. I loop through the database to post all events for the week. I have reused this code many times to display data but the date conversions don't work. I wanted to get the conversion to work before I start bucketing the events by day. Thanks again for your help. <?php if (!empty($result)) { while ( list($key,$val)=each($result) ) { $event_id = stripslashes($val["event_id"]); $title = stripslashes($val["title"]); $event_start = stripslashes($val["event_start"]); $event_end = stripslashes($val["event_end"]); $description = stripslashes($val["description"]); $status = stripslashes($val["status"]); ?> <table width="500" border="0" cellspacing="1" cellpadding="4" bgcolor="#F2F2F2"> <tr bgcolor="#CCCCCC"> <td>Event Details <b><?php print "$event_start"; ?></b></td> </tr> <tr><td> <?php $event_start_converted = date('D', $event_start); print "<br>Event Start Date " . $event_start . "<br>"; print "Event Start Date Converted " . $event_start_converted . "<br>"; ?> <?php print "<b>" . $title . "</b> "; print "<font color=#000066>(" . $event_start . "-"; print $event_end . ")</font>"; // Show icons for editing and closing if event is not closed if ($status <> "Closed") { print "<a href=editevent.php?event_id=" . $event_id . "> <img src=images/edit_icon.jpg alt=Edit border=0></a>"; print "<a href=calendar.php?event_id=" . $event_id . "&closeevent=yes> <img src=images/stop_icon.jpg alt=Complete border=0></a><br>"; } else { print "<br>"; } print $description . "<br>"; print "Status: " . $status; ?> Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615890 Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 you need to use strtotime() on $event_start in that loop you've just posted. see if that fixes your issue. Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615893 Share on other sites More sharing options...
hopeless_reckless Posted August 13, 2008 Author Share Posted August 13, 2008 Thanks for the help. I added back in the strtotime() and now the output looks like this: Event Start Date Wed 8-13 10:00 AM Event Start Date Converted Wed Event Start Date Fri 8-15 12:00 AM Event Start Date Converted Wed Event Start Date Fri 8-15 1:30 PM Event Start Date Converted Wed Thanks! Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615901 Share on other sites More sharing options...
hopeless_reckless Posted August 13, 2008 Author Share Posted August 13, 2008 Just to confirm it's nothing with the loop, I tried this code: $event_start = 'Wed 8-13 10:00 AM'; $event_start_converted = date('D', strtotime($event_start)); print "Event Start Date " . $event_start . "<br>"; print "Event Start Date Converted " . $event_start_converted . "<br>"; $event_start = 'Fri 8-15 12:00 AM'; $event_start_converted = date('D', strtotime($event_start)); print "Event Start Date " . $event_start . "<br>"; print "Event Start Date Converted " . $event_start_converted . "<br>"; $event_start = 'Fri 8-15 1:30 PM'; $event_start_converted = date('D', strtotime($event_start)); print "Event Start Date " . $event_start . "<br>"; print "Event Start Date Converted " . $event_start_converted . "<br>"; and got this: Event Start Date Wed 8-13 10:00 AM Event Start Date Converted Wed Event Start Date Fri 8-15 12:00 AM Event Start Date Converted Wed Event Start Date Fri 8-15 1:30 PM Event Start Date Converted Wed Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615943 Share on other sites More sharing options...
Barand Posted August 13, 2008 Share Posted August 13, 2008 strtotime() can only handle certain date formats. Yours isn't one of those <?php $d = strtotime('Wed 8-13 10:00 AM'); var_dump($d); // --> bool(false) ?> Whereas this one is OK <?php $d = strtotime('Wed 8/13/2008 10:00 AM'); var_dump($d); // --> int(1218618000) ?> Check by converting back echo date('Y-m-d H:i:s', $d); // -> 2008-08-13 10:00:00 Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615952 Share on other sites More sharing options...
akitchin Posted August 13, 2008 Share Posted August 13, 2008 you can simply add another DATE_FORMAT() into your SELECT statement that grabs just the day number, name, whatever of the date in question. this will make life a heck of a lot easier. Link to comment https://forums.phpfreaks.com/topic/119515-date-formating-doesnt-work/#findComment-615954 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.