0

I'm VERY new to PHP and I have been looking into this for a while, and tried a few solutions on here which haven't worked.

I have been making a website for a class at university, a festival website, and I have been trying to display a specific user inputted date (via CMS system) rather than a hardcoded date (which is all I can seem to find solutions for online).

Here is an example of my code:

$query = "SELECT * FROM gatherings";
$select_all_events_query = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($select_all_events_query)) {

    $gather_id = $row['gather_id'];
    $gather_name = $row['gather_name'];
    $gather_date = $row['gather_date'];
    $gather_img = $row['gather_img'];
    $gather_content = substr($row['gather_content'], 0, 150);
    $gather_supporting = $row['gather_supporting'];
    $gather_tags = $row['gather_tags'];
    $gather_status = $row['gather_status'];

    if($gather_status == 'Published') {

?>

<div class="col-sm-6">
            <div class="eventcard-left card">
                <div class="card-body card-body-align text-center">
                    <h1 class="card-title"><?php echo $gather_name; ?></h1>
                    <p class="card-text"><?php echo $gather_content; ?></p>
                    <h1 class="card-title"><?php echo date_format($gather_date,"d/m/Y"); echo "<br>"?></h1>
                    <a class="btn btn-outline" href="event.php?p_id=<?php echo $gather_id; ?>" class="btn btn-primary">View Event</a>
                </div>
            </div>
        </div>
}

No matter what I do it always displays as YYYY-MM-DD, I know it's probably a really simple solution but I've tried so much and can't get it to change. When I create my event I have tried changing this too, no luck. If there's anything else you need to help me out I can provide.

if(isset($_POST['create_event'])) {
    $gather_name = $_POST['gather_name'];
    $gather_date = date('d-m-Y');
}

Also, on the subject of dates and times, I am trying to display times as 00:00, but only returning 00:00:00, hopefully the two solutions are similar.

Caitlin
  • 531
  • 5
  • 19
  • 1
    The first argument to `date_format()` has to be a `DateTime` object, not a string or number that you fetched from a database. – Barmar Mar 30 '20 at 20:34
  • 1
    What is the value of `$gather_date`? – Barmar Mar 30 '20 at 20:35
  • @Barmar I can see that from my error here, this was another failed trial. It was originally just echo $gather_date but this is only displaying YYYY-MM-DD. I am looking to display DD-MM-YY and YYYY in other areas of site. – Caitlin Mar 30 '20 at 20:37
  • The gather date is based on whats inputted via a CMS system, so for example if an event is created that is happening on 25-May-2020 it will display that date, but another event could be 16-Aug-2020. – Caitlin Mar 30 '20 at 20:38
  • 1
    You need to use `strtotime()` to convert that to a timestamp, then use `date()` to format it, not `date_format()`. – Barmar Mar 30 '20 at 20:40
  • Lifesaver! I hadn't seen strtotime() before, thanks for this, new it would be a simple solution! – Caitlin Mar 30 '20 at 20:45

1 Answers1

1

You need to create a DateTime object first. Try:

echo date_create($gather_date)->format("d/m/Y");

This is a short-hand notation to create a DataTime object and then call format() method on it directly. You pass your date string as a parameter to DateTime's constructor or in this case date_create() function.

A longer notation would look like:

echo (new \DateTime($gather_date))->format("d/m/Y");
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thank you, I'm using strtotime() for now, but this is also a very helpful solution for the future! – Caitlin Mar 30 '20 at 20:46
  • @Caitlin I strongly advice against using the old `strtotime()`. It might work for now, but later it will cause you headaches and confusion. `DateTime` class is more versatile. – Dharman Mar 30 '20 at 20:47