exploding date trouble

Discussion in 'PHP' started by alhen, Jun 22, 2007.

  1. #1
    Hi,
    I'm getting stumped with how the explode() function is handling my date.

    I know this is a little backwards, but I'm trying to take a date like this: 2007-06-22

    and explode it into it's respective year, month, day, then rearrange it into US or EU date format. But when I try this:

    list($year, $mon, $day) = explode('-', $row['date']);
    PHP:
    and rearrange it into this:
    $mon-$day-$year
    Code (markup):
    it seems to be doing math instead of just recalling the month, day or year.

    This is kind of new for me, so I'm thinking I just don't know of a rule that I'm supposed to follow, or something... Any advice/help would be much appreciated.

    Thanks, ~alhen
     
    alhen, Jun 22, 2007 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes, you are doing math.. it's not the month / day / year that's the problem, it's the fact that you are using a mathematical operator instead of a 'dash' in a string.

    Without more code I can't tell you exactly, but you're probably after something like:
    $mon . '-' . $day . '-' . $year
     
    TwistMyArm, Jun 22, 2007 IP
  3. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry for lack of code... here's all of it:

            <?
    	$hostname = "localhost";
    	$username = "xxx";
    	$password = "x";
    	$usertable = "xx";
    	$database = "xxx_xxx";
    	$todate = date("Y-m-d");
    
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$database") or die( "Unable to select database"); 
    
    $query="SELECT * FROM $usertable WHERE date >='$todate' ORDER BY date ASC LIMIT 1";
    $result=mysql_query($query) or die(mysql_error());
    
    
    list($year, $mon, $day) = explode('-',  $row['date']);
    
    if (mysql_num_rows($result) == 0) {  echo "<div class=\"test\"><a href=\"gigs.php\"><b>No Performances Scheduled</b></a></div>";
    } else {
      // this is an easier way to loop ove the returned results  /*   * The $row variable is an array with the names of the database fields as keys. For instance you have a field named "date",   * then you can access the value using: $row['date']  */  
    while ($row = mysql_fetch_assoc($result)) {
    echo "<div class=\"test\"><a href=\"gigs.php\"><b>" . $row['day'] . "</b>, <b>$mon-$day-$year</b><br><b>" . $row['location'] . "</b></a></div>";
     }
    }
    ?>
    Code (markup):
     
    alhen, Jun 22, 2007 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    why not just do something like this to convert it?

    
    $a="2007-06-22";
    echo date("m-d-Y",strtotime($a));
    
    PHP:
    just seems more logical to me
     
    ansi, Jun 22, 2007 IP
  5. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks ansi,
    I didn't really know something like this was possible. However, when I tried your code, it just returned today's date... like it only reads
    echo date("m-d-Y");
    PHP:
    instead of
    echo date("m-d-Y",strtotime($a));
    PHP:
    But I believe that something like this should work. Am I missing something? (very possible) :eek:

    ~alhen
     
    alhen, Jun 22, 2007 IP
  6. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #6
    it worked fine for me i dunno. maybe post your whole code and i'll look. here's the output from my bot on irc.

    
    [18:05] <@ansimation> !php $a="2007-6-22"; echo date("m-d-Y",strtotime($a));
    [18:05] <wigga> 06-22-2007
    
    Code (markup):
    now this wont work if it's in Y-d-m format and the d is a valid month.

    for example: 2007-09-06 where it's june 9th, not september 6th. but with the format you specified above there shouldn't be any problems.
     
    ansi, Jun 22, 2007 IP
  7. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you don't mind... will you try using another date besides today's date as "$a" to see if it outputs that date, or still today's date? That's what's happening to me...

    Thanks, ~alhen
     
    alhen, Jun 22, 2007 IP
  8. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #8
    [18:49] <@ansimation> !php $a="2007-10-29"; echo date("m-d-Y",strtotime($a));
    [18:49] <wigga> 10-29-2007
     
    ansi, Jun 22, 2007 IP
  9. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #9
    i assume that you got this working?
     
    ansi, Jun 22, 2007 IP
  10. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Why don't you store the date in the database as a TIMESTAMP, and let mysql return the UNIX timestamp, this value can be filledin into the php date function immediatly, and leaves out the exploding part or converting part (which is kinda ugly).
    $converted_date = date("m-d-Y", $unixtime_from_database);
    PHP:
     
    UnrealEd, Jun 23, 2007 IP