0

I have a field Publish_on in my mysql database table. It stores the date and time when a post was added.

I am not able to get the publish dates. Instead, the output I get is today's date, even if the post belongs to other dates.

$id = (int) ($_GET['postid']);

$result = mysqli_query("
    SELECT Publish_on 
      FROM `post_head` 
     WHERE `post_id` = '" . $_GET['id'] . "'
");

$row = mysqli_fetch_row($result);
$date = date_create($row['Publish_on']);

echo date_format($date, 'g:ia \o\n l jS F Y');  
6
  • 1
    what is the output of your query? Commented Nov 22, 2016 at 5:55
  • today's date and current time @NitinP Commented Nov 22, 2016 at 5:56
  • 1
    Can you please try $date = date_create(strtotime($row['Publish_on'])); Commented Nov 22, 2016 at 5:56
  • its working fine ... Please check live demo : eval.in/682415 Commented Nov 22, 2016 at 5:59
  • What's the point in the $id variable? You never use it. Commented Nov 24, 2016 at 13:13

1 Answer 1

1

If you are going to use mysqli_fetch_row, then you need to use $row[0] instead of $row['Publish_on'].

http://php.net/manual/en/mysqli-result.fetch-row.php

mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array

<?php

  $id     = (int) ($_GET['postid']);
  $result = mysqli_query("SELECT Publish_on FROM `post_head` WHERE `post_id` = '" . $_GET['id'] . "'");
  $row    = mysqli_fetch_row($result);
  $date   = date_create($row[0]);
  echo date_format($date, 'g:ia \o\n l jS F Y');

?>

But if you still prefer to use $row['Publish_on'] then use mysqli_fetch_array or mysqli_fetch_assoc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.