Could not convert datetime format using PHP - php

I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.

date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");

Related

strtotime() in php returning always 01-01-1970

I am using a variable to get the date from the database,then i am passing that variable to strtotime function to get the desired format,but it always returning wrong date.perhaps there is a problem in passing a variable in strtotime function.please suggest me guys,how should i get the correct date in correct format.
Here is what i am trying to do
$date = $fetch_user['date'];
$newDate = date("d-m-Y", strtotime($date));
$day = date('l', strtotime($newDate));
echo $newDate;
echo "-----";
echo $day;
exit;
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
In other words, your strtotime($date) returns 0, meaning that $date is passed in an unsupported format for the strtotime function.
So you'll have to check for yourself $date, before calling strtotime at all.
01-01-1970 means you probably get 0 as a result of strtotime(). You are probably using a format, that this function cannot understand. The PHP documentation states:
The function expects to be given a string containing an English date
format and will try to parse that format into a Unix timestamp (the
number of seconds since January 1 1970 00:00:00 UTC), relative to the
timestamp given in now, or the current time if now is not supplied.
So it is not really flexible. You might want to try DateTime::createFromFormat instead. Take a look at it's documentation.
Basically you have to specify the format of the date string, that you give as an input as well. That way you can use whatever date format you want.
Example from php.net:
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');

PHP strtotime doesnt want to work

If I have the following code:
var_dump(strtotime('2:28:15am 28/11/2013'));
It returns false. What is causing this?
That format is probably not a format that strtotime() can interpret. Try using DateTime::createFromFormat() instead:
$dt = DateTime::createFromFormat('g:i:sa d/j/Y', '2:28:15am 28/11/2013');
echo $dt->format('Y-m-d H:i:s');
I had to guess at the exact formatting of your dates. But you can easily edit that by using the appropriate formatting options listed here.
There is a problem with your time format, strtotime works with the following format for sure:
$date->format('Y-m-d G:i:s');
strtotime($date);

Display Date dd/mm/yy in php with time from date/time

I store the date and time in mysql as a date/time field which has this format: 2012-03-12 14:51:26, what i am trying to do is simply rearrange the DD/MM/YY to look like this.
When i use the following code, it just gives me a date wrong format warning.
echo date_format($date, 'Y-m-d H:i:s');
If you are just displaying the date you can supply a certain format in the SQL query
SELECT DATE_FORMAT("%d/%m/%Y", date_column) FROM table
If you convert the MySQL timestamp to a unix timestamp, then you can use the date() function to output it in whatever format you like:
$unixTimestamp = strtotime($mysqlDate);
echo date($dateFormat, $unixTimestamp);
See the date format strings here: http://php.net/manual/en/function.date.php
First, convert it to a Unix timestamp (which I find to be all around better than a date_time field for a lot of reasons), then use PHP's date function.
echo date('Y-m-d h:i:s', strtotime($date));
Simply do:
date("d/m/Y", strtotime($date));
And read about strtotime function.
this will work.
$date = date_create("2012-03-24 17:45:12");
echo date_format($date, 'Y-m-d H:i:s');

PHP Date from strtotime with current time

I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');

Creating date in PHP

I need to convert this date:
10.04.2011 19:00
To a date variable that I can use in PHP.
Can someone help me with that? I tried this way:
$dateConverted = date("d.m.Y H:i",strtotime ($date));
But it returns 01.01.1970 00:00
DateTime::createFromFormat() to the rescue!
It looks like your format is d.m.Y H:i.
So, this should work for you:
$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');
You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.

Categories