Strange date issue with PHP strtotime - php

I'm trying to store some dates in my datebase (MySQL). But I got a strange conversion error:
This is my piece of PHP code:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=date('Y-m-d', strtotime(str_replace('/', '-', $fechanac)));
echo "<h1>{$fechanac}</h1>";exit();
See the following three example, trying with (01/01/1900, 01/01/1901 and 01/01/1902).
OUTPUT:
01/01/1900
1970-01-01
01/01/1901
1970-01-01
01/01/1902
1902-01-01
Somebody know why happens this? And how to fix it? I need to insert in my DB, possible dates of living persons. Thanks for reading.

Date stored!
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$datearray=explode("/",$fechanac);
$fechanac="{$datearray[2]}-{$datearray[1]}-{$datearray[0]}";

I know it's and old post, but maybe this late answer can help others:
The DateTime class correctly manages dates older than 1970. So forget the use of date() and evolve to DateTime:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=(new DateTime($fechanac))->format('Y-m-d');
echo "<h1>{$fechanac}</h1>";exit();

Related

Convert date time to timestamp using php

I am quite sure this has been discussed before but for some reason mine is not working.
I am trying to convert date time to a timestamp.
echo strtotime("18 May 2.50pm");
The above code returns blank.
Any help is highly appreciated. Thanks in advance.
Use php DateTime::createFromFormat to make DateTime
<?php
$date = DateTime::createFromFormat('j M h.ia', '18 May 2.50pm');
echo $date->getTimestamp();
//print_r($date);
?>
Live Demo
Now you can use method available for DateTime .
Note : As you have not given year it will give you result for current year

Unable to understand date function in PHP

Based on the definition given on php.net about function date(), I tried to use it in the code like this:
echo"The date is :".date("l, d/m/Y, h:i:s a", time());
But the timestamp doesn't get printed. Why is this? I am unable to understand this part of the function.
To print timestamp just use time()
echo time();
I understood the problem:
I had to written the code properly but since It was not displaying current date and time, I was confused. I set the time zone to Asia/Kolkata. Now its working fine.
Thank you everyone for help.

Timestamp Difference and Output

In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.

php function for linux time conversion

I'm new to PHP and I need a PHP function that converts Linux times like 1396310400 from $variable and returns a human-readable date like 1. apr. 2014! (time of day not needed)? Any suggestions is much appreciated.
echo date('d/m/Y', $time_linux);
read here: http://php.net/manual/en/function.date.php to learn how to modify the result to show exactly what you want.
Thanx for your input.
This worked and returned a human readable date into my wpallimport run - at first I just did not understand the syntax:
function overtagelsesdato($time_linux) {
echo date('d/m/Y', $time_linux);
}
thank you OfirH!

date format in MySQL table

I have the following two names in MySQL table:
message is type text
date is type datetime
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><div><a><? echo $row['message'] ?></a></div></td>
<td><div><a><? echo $row['date']; ?></a></div></td>
</tr>
<?php
}
?>
The output of the above table shows date in this format 2012-08-05 17:43:57
How to print date in this format 2012-08-05 (without time)?
I cannot change type datetime in MySQl (it’s required in different pages in different formats).
I’ve tried the following, but it doesn’t help (no printing at all)
<td><div><a><? echo $rows['date'](“Y-m-d”); ?></a></div></td>
p.s. other combinations of date format give me syntax error.
Any suggestion would be much appreciated,
You want
<?php echo date('Y-m-d', strtotime($row['date'])); ?>
strtotime converts your datestring into a unix-timestamp
then, the date function formats it properly according to the string.
You want the date() to change the format.
There is something called TO_CHAR method in MS-SQL and Oracle in which you can pass the format you want your date in, so while fetching the data you can change it
You should use format function
echo new DateTime($row['date'])->format('Y-m-d');
UPDATED thanks to #Erty
http://php.net/manual/en/datetime.format.php
<td><div><a><? echo new DateTime($row['date'])->format('Y-m-d'); ; ?></a></div></td>
every one else is suggesting you do it with php, i always do this with mySQL's own date function:
SELECT ... DATE_FORMAT(Date_Field ,'%Y-%e-%c') ...
DATE_FORMAT
You first need to convert it to a date:
$tmpdate = strtotime($row['date']);
$date = date('Y-M-d', $tmpdate);
echo $date;
Of course you may short it down, as others suggested, but i prefer "the long way" to improve readability and help you get up to speed faster when you go back and review old code in the future. :-)

Categories