How can I avoid displaying 1970-01-01 when the database field is empty? Since my webpage is in Europe I need to display the date like this dd.mm.YYYY using this code:
<?php echo date("d.m.Y", strtotime($row['date'])); ?>
But where the db fields are empty I get the 1970-01-01. I understand why this is happening, but is there a way to avoid this and display a blank field on the webpage insted?
Original format of the date in the db is YYYY-mm-dd
You could do something like this:
$date = empty($row['date']) ? "" : date("d.m.Y", strtotime($row['date']));
echo $date;
Try this:
<?php
if(isset($row['date']) && $row['date'] !="")
{
$date = date("d.m.Y", strtotime($row['date']));
}
else {
$date = "";
}
echo $date ; ?>
Related
I have a problem, the date is saved in the form 1556447923594 (microdate). I want to retrieve this date from the database and display it in php.
$datetime = gmdate("Y-M-D G:i:s", $row['time']);
if($row['time'] == "0"){
echo "<td>Permban</td>";
}
else
{
echo "<td>$datetime</td>";
}
And I have = 51290-Aug-Fri 18:38:20 not this 2019-04-28 01:22:12
You just need to divide the value by 1000 to convert it to seconds so it can be used by gmdate. Also, to get the output in the form you want, you should change the format string to Y-m-d H:i:s:
$row['time'] = 1556447923594;
echo $datetime = gmdate("Y-m-d H:i:s", $row['time']/1000);
Output:
2019-04-28 10:38:43
Demo on 3v4l.org
Ive checked the leads here and have not found the right solution,
<?php
$mysqldate = $row['callbackdate'];
$phpcurrentdate = strtotime( $mysqldate );
if ( $phpcurrentdate == date("Y-m-d") ) {
echo $row['last_name'];
}else{
echo "Nothing";
}
?>
The date field in sql is date and the format is YYYY-mm-dd. The answer always being returned is "Nothing". I know this sort of question has many variations but Ive had no luck finding this type. Im not looking for a range sort just a simple match is all... and the data $row['callbackdate']is functioning as Ive tested all other connections.
So Id appreciate any help! Thanks,
Les
$mysqldate = "2017-08-28"; //example
$phpcurrentdate = DateTime::createFromFormat('Y-m-d', $mysqldate); //put your format
$today= new DateTime("now");
if ( $phpcurrentdate == $today ) {
echo 'last_name';
}else{
echo "Nothing";
}
The dates are stored as string in your database. As possible solution to this is formatting both as dateTime for example:
$today = date('Y-m-d H:i:s');
$databaseDate = '2017-08-06 20:50:38';
var_dump(new DateTime($today) > new DateTime($databaseDate)) // returns true
var_dump(new DateTime($today) < new DateTime($databaseDate)) // returns false
Another way to do this is to make your columns type in the database a DateTime. This way you only have to format the current date as a DateTime and then you can compare them
/* yeardate = 2000 monthdate = 02 daydate = 02 */
$date = new DateTime($yeardate.'-'.$monthdate.'-'.$daydate);
echo date('Y/m/d', strtotime($date));
Keeps echoing 1970/01/01 or something on the lines of that.
here you are, try to use proper class like below:
<?php
$date = DateTime::createFromFormat('Y-m-d', '2000-02-02');
echo $date->format('Y/m/d');
?>
Here you can use php date function,
echo date('Y-m-d');
I am using this function to change date format.
echo date("mdy", strtotime('2013-11-04'));
It's return: 110413
Now i want to decode it.
How to get date like this format: 2013-11-04
Remember 110413 should be as a input of that function
Thanks
Try like this
<?php
$MyDate="110413";
$date = DateTime::createFromFormat('mdy',$MyDate);
echo $newformat=$date->format('Y-m-d');
?>
$mydate=date("Y-m-d", strtotime('2013-11-04'));
echo date("Y-m-d", strtotime($mydate));
try like this,
$date = date("mdy", strtotime("2013-11-04"));
$myDateTime = DateTime::createFromFormat('mdy', $date);
$newDateString = $myDateTime->format('Y-m-d');
echo $newDateString;
I need to insert the datetime value entered from the HTML form using PHP into the MySQL database. However I receive the Incorrect datetime value error each time I try to execute the code below,
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
echo date('d/M/Y H:i:s', $date);
// echo DATE_FORMAT($rosterstartdate,"%Y%m%d %H%i%s");
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts','$date','$rosterenddate')";
I have tried using each of the different techniques above to do the conversion but it does not work. Any ideas on how this could be inserted
try this:
$date = date('Y-m-d H:i:s', $date);
Instead of echoing it out, use that code to format the date.
However, it looks like what you really want is this:
$rosterstartdate = date('Y-m-d H:i:s', strtotime($rosterstartdate));
$rosterenddate = date('Y-m-d H:i:s', strtotime($rosterenddate));
This way you can just reference those two variables in your query string.
You don't need to format it if you have a unixtime use FROM_UNIXTIME,
change your query as
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts',FROM_UNIXTIME($date),'$rosterenddate')";