Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Inconsistant PHP behavior?

Inconsistant PHP behavior?
Thread Tools
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 16, 2007, 12:14 PM
 
I'm currently working on a PHP/MySQL project that, among other things, involves dates. I have a function that I wrote a long time ago that I've been using for years which converts MySQL dates and datetimes to a more readable format. It's never given me problems before, but not, for some reason, it is.

Basically you give the function a MySQL datetime such as '2007-03-16 13:11:30' and it returns 'Mar. 16, 2007; 1:11 PM'. But for some reason when the page is on my client's server it's returning a string more like '. 13, 2007; 139 PM'. I know the MySQL date is being set correctly, because it's being set as NOW() rather than manually. And the code is identical, so I can't figure out what's causing this problem.

Has anyone encountered anything similar or have any ideas of how I could fix this?

Here's the function:
[codex]function fix_date($sql_date, $US = 1, $time = 0) {
$months = array(1 => "Jan", 2 => "Feb", 3 => "Mar",
4 => "Apr", 5 => "May", 6 => "Jun",
7 => "Jul", 8 => "Aug", 9 => "Sep",
10 => "Oct", 11 => "Nov", 12 => "Dec");

$year = substr($sql_date, 0, 4);
$month = (int)substr($sql_date, 5, 2);
$day = substr($sql_date, 8, 2);

if (substr($sql_date, 0, 10) == "0000-00-00" || $year == "9999" || $sql_date == "" || is_null($sql_date))
$date = "TBD";
else {
if ($US)
$date = "$months[$month]. $day, $year";
else
$date = "$day $months[$month] $year";
}
if ($time) {
$date .= "; ";
$hour = (int)substr($sql_date, 11, 2);
$date .= $hour > 12 ? $hour - 12 : $hour;
$date .= substr($sql_date, 13, 3);
$date .= $hour >= 12 ? " PM" : " AM";
}

return $date;
}[/codex]

(I really wish that would preserve indentation...)
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Mar 16, 2007, 12:37 PM
 
I think your code is much more complicated than it has to be.

To convert 2007-03-16 13:11:30 to 'Mar. 16, 2007; 1:11 PM', all you need is this:

<?php
$input_date = "2007-03-16 13:11:30";
$output_date = date('M. d, Y; h:i A', strtotime($input_date));
?>
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 16, 2007, 12:41 PM
 
*smacks forehead*

Thanks, I feel like an idiot. I guess that's what happens when you keep reusing code you wrote a long time ago. It never even occurred to me to bother improving it until now.

Now to see if the new version works on the client's server properly as well.
(Last edited by nonhuman; Mar 16, 2007 at 12:47 PM. )
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Mar 16, 2007, 12:46 PM
 
strtotime is very useful, it will convert many different kinds of date formats to a Unix timestamp. The PHP "date" function will allow you to format a date in any desired format based on either today's date, or a Unix timestamp:

PHP: date - Manual

You can do date calculations (e.g. 1 month from now) via date and mktime, e.g.

1 month from now:

<?php
$today = explode('-', date('Y-m-d'));
$output_date = date('Y-m-d', mktime(0, 0, 0, $today[1] + 1, $today[2], $today[0]));
?>
This will output 2007-04-16, but you can substitute your own date format for Y-m-d
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:48 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2