Jump to content

Date formating doesn't work


hopeless_reckless

Recommended Posts

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

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"

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

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;
		?>	

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!

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

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 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.