date format from mysql database - php

I have a column type DATE in mysql database and want to get date in format like this - 21-jan.
$stmt = $db->query('SELECT id, date, title, category FROM posts ORDER BY date DESC');
while($row = $stmt->fetch()){
$date = strtotime($row['date']);
$date = date_format($date, "d-M"); // line 49
Warning:
date_format() expects parameter 1 to be DateTimeInterface, integer given... on line 49
Any help ?

Try to do this:
$date = strtolower(date("d-M", $date));
Take a look at date function:
http://php.net/manual/pt_BR/function.date.php

Use date function instead of date_format:
$stmt = $db->query('SELECT id, date, title, category FROM posts ORDER BY date DESC');
while($row = $stmt->fetch()){
$date = date("d-M", strtotime($row['date']));
$formatedDate = strtolower($date);

Related

Update Mysql row where date equal to todays date

I am trying to update a row in my table where the date in a column is equal to today's date.
I have today's date in a variable $currentDate and when I echo this out it is displayed on the screen in the following format
2020-05-08
Which looks same format as the Db table, but I still get the error invalid datetime format.
Below is the code I'm using. Any help please
$currentDate = date("Y-m-d");
$currentTemp = 33;
echo $currentDate;
$sql = "UPDATE weather_station SET currentTemp = $currentTemp WHERE date = $currentDate";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$sql = "UPDATE weather_station SET currentTemp = :temp WHERE date = :currenDate";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":temp", $currentTemp);
$stmt->bindParam(":currenDate", $currentDate);
$stmt->execute();
This works now

Search data between two dates in dd/mm/yyyy hh:mm:ss in sql

I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh

delete string date on database

I have a table (db_dates) with three columns (id, datetime and name_date).
I want to delete one row, when the date is over, such likes this:
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE '".$rowTime[0]."' < '".$today."'");
}
Not working, how do I do that? It is always deleted all data!
I think this will help you.
Because as per your code date will be always less then today's date the way you are getting.
So you should try below code using DATEDIFF().
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE DATEDIFF('".$rowTime[0]."', NOW()) < 0");
}

Sorting blog posts by date, conversion failed

I am building a blog and im trying to do so that i can sort by year and month, however i get the error:
SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL
Server]Conversion failed when converting date and/or time from
character string.1
Obviously i am using Microsoft SQL server just to make it clear.
i have sorted that when showing the post by doing like this
date_format( new DateTime($postdate['5']), 'd M Y, H:i' );
so how do i implement a thing like that on this piece?
if (isset($_GET['year_month']))
{
$bdate = $_GET['year_month'];
$tsql4 = "SELECT * FROM blog_posts WHERE blog_date=:bdate ORDER BY blogID DESC";
$stmt5 = $conn->prepare($tsql4);
$stmt5->execute (array($bdate));
while($postdate = $stmt5->fetch(PDO::FETCH_BOTH) )
{
here i post the $postdate rows
i have tried some SELECT convert blablabla but havent gotten it to work..
The dates is stored in the db like 2016-01-01 HH:MM:SS
If $postdate['5'] equals to $_GET['year_month'] then you can use this code:
if (isset($_GET['year_month']))
{
// $_GET['year_month'] looks like '2016-02';
list($year, $month) = #explode('-', $_GET['year_month']);
$day = 1;
$datetime = new DateTime();
$datetime->setDate($year, $month , $day);
$datetime->setTime(0, 0, 0);
$bdate_start = date_format($datetime, 'Y-m-d H:m:s');
$datetime->add(new DateInterval('P1M'));
$bdate_finish = date_format($datetime, 'Y-m-d H:m:s');
$tsql4 = "SELECT * FROM blog_posts WHERE blog_date BETWEEN :bdate_start AND :bdate_finish ORDER BY blogID DESC";
$stmt5 = $conn->prepare($tsql4);
$stmt5->execute (array($bdate_start, $bdate_finish));
while($postdate = $stmt5->fetch(PDO::FETCH_BOTH) )
{
here i post the $postdate rows

filter date from timestamp field in mysql

I have saved rows in my table with custom timezone.Now I want to retrieve data from those table for just today.
So here is what I tried
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
And my row contains date in timestamp format like 2015-12-07 22:42:02
But I get empty result.
Try this:
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );
to convert time according to timezone: ConvertTimeZone
if $today='2015-12-07 22:42:02'; your query will give the result.
$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
else do pass the today date and next date and retrieve the value as given
$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next' " );
for more details refer this Oracle SQL : timestamps in where clause How to compare Timestamp in where clause
You should convert date to timestamp before passing it to mysql:
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );

Categories