Viewed   108 times

Crashes on:

<?php 
    $date = "13-06-2015 23:45:52";
    echo Datetime::createFromFormat('d-m-Y h:i:s',  $date)->format('Y-m-d h:i:s'); 
?>

PHP Fatal error: Call to a member function format() on boolean

But with other dates works well:

<?php 
    $date = "10.06.2015 09:25:52";
    echo Datetime::createFromFormat('d-m-Y h:i:s',  $date)->format('Y-m-d h:i:s');
?>

Wrong format?

 Answers

2

Neither example work as you have multiple errors:

  1. You forgot your second parameter to Datetime::createFromFormat()
  2. h:i:s should be H:i:s
  3. Your date in the second example is separated by a . not a -

Fixes:

<?php 
    $date = "13-06-2015 23:45:52";
    echo DateTime::createFromFormat('d-m-Y H:i:s', $date)->format('Y-m-d h:i:s'); 

    $date = "10.06.2015 09:25:52";
    echo DateTime::createFromFormat('d.m.Y H:i:s', $date)->format('Y-m-d h:i:s');
?>
Sunday, December 25, 2022
3

convert it to a PHP date object with strtotime() then output it with date()

EDIT

Some more detail; try:

$time = strtotime('Tue Jan 05 11:08:27 +0000 2010');
echo date("Y-m-d h:i", $time);

Y = 4 digit year m = 2 digit month (with leading 0) d = 2 digit month (with leading 0)

h = 12 hour time (leading 0) i = minutes (with leading 0)

http://php.net/manual/en/function.date.php for all the formatting options

Tuesday, August 2, 2022
 
explv
 
3

You need to convert it to something you can use for further formatting. strtotime() is a good start, which yields a unix timestamp. You can format that one using strftime() then.

strftime("%d-%b-%G", strtotime($ts));
Tuesday, November 1, 2022
 
3

The answer was due to hidden characters located in the lessons.db file.

The error shown had nothing to do with this and I would like to thank everyone who took the time to give their two pence.

Wednesday, November 23, 2022
 
bartosz
 
2

As I stated in comments, you didn't choose a database.

As requested:

This line:

$db = new mysqli("localhost","root","");

Should have read as:

$db = new mysqli("localhost","root","", "database");

and replacing "database" with the name of your database.

The documentation can be found at the following address on PHP.net:

  • http://php.net/manual/en/function.mysqli-connect.php

Example pulled from the manual along with error checking:

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

Note: Alhough you can still theoretically connect, you just wouldn't be able to query anything though. That's where error checking is important during development testing.

Friday, August 26, 2022
 
4

No, there is nothing built-in for Dateobjects, but there are a bunch of libraries to deal with and format them:

  • date.js
  • moment.js
  • XDate
  • Date and Date.Extras in Mootools
  • Date in Sugar
  • Dojo.date
  • a few functions in Mochikit
  • DateFormat (only PHP's date)
  • date at php.js
  • DataType in YUI, especially for i18n
  • date-functions.js, used especially by JQuery datetimepicker plugin
Thursday, August 18, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :