0

I want to display $eventdate (saved as a unix time INT value on SQL server), in the format DD/MM/YYYY (or similar).

Thanks!

    ...
    $entry_display .= <<<ENTRY_DISPLAY
    <div class="post">
    $eventdate
    </div>
    ...

EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:EDIT:

Heres the whole code:

<?php
class simpleCMS {

  var $host;
  var $username;
  var $password;
  var $table;

  public function display_public() {
    $q = "SELECT * 
          FROM eventsDB
          WHERE eventdate > UNIX_TIMESTAMP()
          ORDER BY eventdate ASC
          LIMIT 30";
    $r = mysql_query($q);

    if ( $r !== false && mysql_num_rows($r) > 0 ) {
      while ( $a = mysql_fetch_assoc($r) ) {
        $title = stripslashes($a['title']);
        $author = stripslashes($a['author']);
        $bodytext = stripslashes($a['bodytext']);
        $eventdate = stripslashes($a['eventdate']);
        $created = stripslashes($a['created']);


        $entry_display .= <<<ENTRY_DISPLAY

    <div class="post">
        <table class="eventstable" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="media/icons/icon_calendar.gif"/>  <b>$title </b></td>
    <td class="right">echo date("Y-m-d H:i:s", $eventdate); </td>
  </tr>
  <tr>
    <td colspan="2" class="small">$bodytext <i>by $author</i></td>
  </tr>
</table>
    </div>

ENTRY_DISPLAY;
      }
    } else {
      $entry_display = <<<ENTRY_DISPLAY

    <h2> Your brand new Events Page! </h2>
    <p>
      No entries have been made yet.
      Follow my instructions to make a new event!
    </p>

ENTRY_DISPLAY;
    }
    $entry_display .= <<<ADMIN_OPTION

    <p class="admin_link">
      <a href="{$_SERVER['PHP_SELF']}?admin=97538642"></a>
    </p>

ADMIN_OPTION;

    return $entry_display;
  }

  public function display_admin() {
    return <<<ADMIN_FORM

    <form action="{$_SERVER['PHP_SELF']}" method="post">

      <label for="title">Title:</label><br />
      <input name="title" id="title" type="text" maxlength="150" />
      <div class="clear"></div>

      <label for="bodytext">Body Text:</label><br />
      <textarea name="bodytext" id="bodytext"></textarea>
      <div class="clear"></div>

      <label for="author">Author:</label><br />
      <input name="author" id="author" type="text" maxlength="100" />
      <div class="clear"></div>

      <label for="eventdate">Date (DD/MM/YY):</label><br />
      <input name="eventdate" id="eventdate" type="text" maxlength="100" />
      <div class="clear"></div>

      <input type="submit" value="Create This Event!" />
    </form>

    <br />

    <a href="../events.php">Back to Events</a>

ADMIN_FORM;
  }

  public function write($p) {
    if ( $_POST['title'] )
      $title = mysql_real_escape_string($_POST['title']);
    if ( $_POST['bodytext'])
      $bodytext = mysql_real_escape_string($_POST['bodytext']);
    if ( $_POST['author'])
      $author = mysql_real_escape_string($_POST['author']);
    if ( $_POST['eventdate'])
      $eventdate = strtotime($_POST['eventdate']);
    if ( $title && $bodytext && $author ) {
      $created = time();
      $sql = "INSERT INTO eventsDB VALUES('$title','$bodytext','$created','$author','$eventdate')";
      return mysql_query($sql);
    } else {
      return false;
    }
  }

  public function connect() {
    mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
    mysql_select_db($this->table) or die("Could not select database. " . mysql_error());

    return $this->buildDB();
  }

  private function buildDB() {
    $sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS eventsDB (
title       VARCHAR(150),
bodytext    TEXT,
created     VARCHAR(100),
author      VARCHAR(100),  
eventdate   INT(15),
)
MySQL_QUERY;

    return mysql_query($sql);
  }
}
?>
hamishl
  • 15
  • 4
  • See http://stackoverflow.com/questions/2904256/how-can-i-convert-bigint-unix-timestamp-to-datetime-in-sql-server/2904288#2904288 – pilcrow Oct 20 '11 at 19:50
  • I didn't get to check back until this morning - I edited my answer on the original question to include the answer to this as well. =) – WWW Oct 21 '11 at 20:19

4 Answers4

1
echo date("Y-m-d H:i:s", $eventdate);

is all you need.

Syntax|: string date ( string $format [, int $timestamp = time() ] )

Peter
  • 16,453
  • 8
  • 51
  • 77
1

Just throw it through the date() function.

$string = date("d/m/Y", $eventdate);
Whetstone
  • 1,199
  • 8
  • 8
  • ive added the entire code, could you tell me where to add this? – hamishl Oct 20 '11 at 19:54
  • You want to add it directly after you receive the $eventdate value. In this case, it would be after $eventdate = stripslashes($a['eventdate']); As a sidenote, you'll notice that at this point (before my code) $eventdate) is still a string, so you need to convert it to a date object first with $eventdate = [strtotime($eventdate)](http://php.net/manual/en/function.strtotime.php). Make sure you start echoing $string (or whatever you use to store it) after instead of $eventdate! – Whetstone Oct 20 '11 at 20:01
0

Have you tried using the date function? Looks like it does exactly what you need.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
0

If i understand you right, you would use the date() function.

$entry_display = date('m/d/Y', UNIX_TIMESTAMP_HERE);

You realize .= is a way to append, right? Without more code i am not sure what your final intent is, but that should answer you?

More info: http://php.net/manual/en/function.date.php

Austin Best
  • 1,028
  • 4
  • 14
  • 30