Date functions in PHP - php

Another simple question. I found this really cool snippet of code:
$date_str = "Jan 14th 2011";
$date = date_parse_from_format('M jS Y', $date_str);
echo $date->format('Y-m-d');
But when I run it on my computer it says Fatal error: Call to a member function format() on a non-object line 3.
The code was taken from here Converting date string to date so I presume it is good but it looks like it is referencing to an object that does not exist.
I have been looking at http://php.net/manual/en/function.date-parse-from-format.php and http://www.w3schools.com/php/php_ref_date.asp amongst many others but I have not found any any clues.
My question is should this code work as a standalone piece of code. If so why does it not work for me? Else, should I do to get it to work as expected.

date_parse_from_format();
returns associative array and you are trying to access the class method on a non object.
if you want to make use of PHP's inbuilt DateTime class. then more information here http://in2.php.net/manual/en/datetime.format.php

date_parse_from_format returns an array, not a DateTime object. What you want is
$date = date_create_from_format('M jS Y', $date_str);
^^^^^^---note the change
echo date('Y-m-d', $date);

Related

PHP DateTime error; cannot parse string

I have this DateTime cannot parse string error in php. i have tried applying solutions I read online but none has worked so far. My question is, is it possible for DateTime() to be disabled. Even the simplest code
$date = new DateTime('2010-5-5');
echo $date->format('Y-m-d);
Does not work.
I also tried using the static function createFromFormat(). And also I am not calling the DateTime class from a Class..so I do not need a back slash. What can I do please
Use this one for date format, So you can give the input format and get output format also
$date = DateTime::createFromFormat('d/m/Y', $date);
$fromdate = $date->format('Y-m-d');

formatting datetime from mysql in php

I am trying to format 2014-03-27 00:53:31 to be: 03/27/2014 I have tried many solutions but none of which have worked. My most recent is explained in this question.
So the time is stored in database as a datetime like: 2014-03-27 00:53:31
I call this by $customer->last_login;
Then I am trying to format this by doing the following:
$dt = $customer->last_login;
echo $dt->format('m/d/Y');
When I run this I get the following error:
Fatal error: Call to a member function format() on a non-object
What am I doing wrong? Or what is a better solution to formatting this to display just the date?
If $customer->last_login is a string, you have to convert to a DateTime object first.
$dt = new DateTime($customer->last_login);
echo $dt->format('m/d/Y');
Try this
echo date('m/d/Y',strtotime($dt));

Can't get around this error: Call to undefined method DateTime::createFromFormat()

I am trying to convert date formats in php.
The following code creates persistent errors
$myDate = "31/12/1980";
$myDateTime = DateTime::createFromFormat('d/m/Y', "$myDate");
$newDate = $myDateTime->format('d M Y');
echo $newDate;
The line containing createFromFormat() keeps creating the error: " "call to undefined method".
This occurs both with my testing Apache server and the actual server, both running PHP 5.3+
Do I need to include or require additional files?
Please help - I am only a low-intermediate in php.
The only two possible reasons why you should be getting this error are:
You're not in fact using PHP 5.3+, so that method does not exist. Double check what PHP version your code is running on. Maybe you have an oops in your web server configuration. If that is indeed so and you cannot change it, see PHP DateTime::createFromFormat in 5.2? for alternatives.
You're in a namespace and need to call it like \DateTime::createFromFormat(...).
This usually occurs if the method cannot be found, meaning you haven't included the file that contains this method. Can you post code for DateTime::createFromFormat?
I have found the solution.
The correct syntax for the date conversion is:
$myDate = "01-12-1980";
$tempDate = date_create("$myDate");
$newDate = date_format($tempDate, 'j M Y');
echo "$newDate";
Produces output: 1 Dec 1980
(j instead of d removes the leading zero from the day number)
Please note that:
$newDate = date("d M Y", strtotime($myDate));
would not work in this example because the expected input format is mm/dd/yyyy, using any other format will produce incorrect output dates

Elegantly change 2011-10-31T16:22:00 to 2011-10-31 in php

I was wondering if anyone could help me out with this.
I am running PHP Version 5.2.16.
Up until now I have used a substring.
$Date = substr($Date,0,10);
$Date = mysql_real_escape_string($Date);
I am scraping this string from an REST Api, so I have no control over its format, and I am not sure if the 'T' in the middle of the string could cause a problem.
Now this does the job simply enough but I was looking to use something more elegant like
$Date = Date::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
but this just returns the error:
Fatal error: Class 'Date' not found in...
Im quite new to php but I am assuming that I require the Date class (Common sense) but how would I implement this class into my script?
Any help or suggestions would be greatly appreciated.
Thanks :)
You could do:
<?php
$Date = strtotime("2011-10-31T16:22:00");
$converted = date("Y-m-d", $Date);
echo ($converted);
?>
Look here http://codepad.org/TNIwXDRp
The class is called DateTime
$Date = DateTime::createFromFormat('Y-m-dTh:i:s', $Date)->format('Y-m-d');
http://php.net/book.datetime
However, because the format is standardized (ISO 8601) it should not surprisingly change, so it should be safe to just split it into date and time yourself
list ($date, $time) = explode('T', $string);
The class is not called Date but DateTime and the createFromFormat() method requires PHP/5.3 or greater. Whatever, the format you have is correctly recognised by the constructor so all you need is:
<?php
$date = new DateTime('2011-10-31T16:22:00');
echo $date->format('Y-m-d');
You can either use substr to extract, or simply use strtotime. I have used both and they are both useful solutions. strtotime would be better if you also would need to extract the timestamp.

PHP Datetime not working properly

I am retrieving the datetime data from mysql the retrieved data from a single row is.
2011-04-11 19:31:30
I wanted to reformat the datetime in d-m-Y H:i:s for that I am using date_format() the below code works just fine.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
However, I don't want to go object oriented way just for reformatting because I will be using the code within the foreach loop and that means I would have to initialize the DateTime class again and again.
I tried doing this the Procedural way using the following code.
$date = $users['registerDate'];
echo date_format($date, 'Y-m-d H:i:s');
the above code does not work for me and gives back the following error.
Warning: date_format() expects parameter 1 to be DateTime, string given in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/users/users.php on line 21
What could be possibly wrong?
The given solution works perfectly fine for the procedural way.
echo date('m-d-Y',strtotime($users['registerDate']));
However I would like to know which will be the best feasible solution the above procedural way or the OOP way.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
Considering I will be using the code within a foreach loop and it may loop for over a hundred times.
You do not need "date_format()":
echo date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
//results: 11-04-2011 19:31:30
You're trying to use a function/object that is part of the DateTime class without creating a reference to the DateTime class.
For procedural formatting take a look at date()
What you are seeking for is this:
date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
Have a look at the php-manual. However, using the methods you yourself proposed is pretty fine, since the DateTime-object maps to some functions written in C.
Also PHP Datetime is working properly, since date_format is just an alias of Date::format, which does exactly require what you don’t want to pass in (a DateTime-object).
Honestly, we’re talking about PHP...

Categories