When I retrieve a date from a my_sqli query and format it it becomes one day forward. The date is saving correctly to the server, and echoing it before the new format is correct.
$date = "SELECT date FROM blogtable WHERE id = $artID";
$dateEx = mysqli_query($con, $date);
while($dateGet = mysqli_fetch_array($dateEx))
{
//This is in YYYY-mm-dd
$dateGet['date'];//If I echo this, it is correct
}
$source = $dateGet;
$newDate = new DateTime($source);
echo $newDate->format('M-d-Y');
So for example if I tried to use it today(the 24th), it would save correctly, but after the format, display as the 25th.
Wrikken's suggestion worked, changing the statement in the while to $source=$dateGet['date']; and deleting the $source = $dateGet;.
Related
So I am working on a simple website and I ran into a problem. I have a subscription based website and I have a date expired for when their subscription ends. This all works well, but when I tried to display the expiration date I ran into problems. The first 3 lines are what i have been trying. It seems as if the timestamp isnt correctly being transferred from the database because when I did my test at the button, this displayed the correct date. The top 3 lines always give me this: 1970/01/01
// Get Expiration Date
// Always gives me 1970/01/01
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$timestamp = mysqli_query($link, $datexpire);
$date = date("Y/m/d",$timestamp);
//This works
$timestamp2 = 1537847863;
$date2 = date("Y/m/d",$timestamp2);
If anyone could help that would be much appreciated
i think your code should be something like
$datexpire = "SELECT date-expire FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row=mysqli_fetch_assoc($result));
$timestamp = $row['date_expire'];
$date = date("Y/m/d", $timestamp);
echo $date;
please check for column name.. if it is date-expire or date_expire ??? (dash or underscore ??)
mysqli_query returns a mysqli_result object or a boolean value.
You want to fetch a row from your given object, like so:
$datexpire = "SELECT `date-expire` FROM users WHERE username='{$_SESSION['username']}'";
$result = mysqli_query($link, $datexpire);
$row = mysqli_fetch_assoc($result);
$date = date("Y/m/d", $row["date-expire"]);
I need to get only date from datetime variable. I was using this code. It was working fine but now I am facing problem when I'm trying to store only date in mysql please. Take a look at the code below and please tell me what I'm missing,
$date_time = "11-Dec-13 8:05:44 AM"
From the date I got from user input, I need to save only date in att_date variable.
$arr = explode("/", $date_time);
$arr2 = explode(" ", $arr[2]);
$att_date = $arr2[0].'-'.$arr[0].'-'.$arr[1];
Here is the solution:
$date_time = "11-Dec-13 8:05:44 AM";
$new_date = date("Y-m-d H:i:s",strtotime($date_time));
Even if you just use "Y-m-d" it will work instead of using "Y-m-d H:i:s",please practice below example:
$datetime = '2022-10-11 06:38:02';
$getOnlyDate = date('Y-m-d',strtotime($datetime));
echo $getOnlyDate; // output: 2022-10-11
You can get the date from the datetime variable using the
Date();
function from mysql using php query, it automatically extracts the date from it. You can learn it here.
I'm making a news website for games and I want a page where you can find all the news subject. So I decided to use it like a lot of webpages do.
Example:
Now I want it like that, but i don't know how to make that like that. I've made a little beginning and I hope you guys can help me with this!
$query = mysql_query("SELECT * FROM `gb_news` order by `datetime` DESC");
while($news = mysql_fetch_array($query)) {
echo $news['title'];
echo $news['datetime'];
}
It's a really simple code, but I only want to know how I add these days when an artical has a new date in the database.
Store the date outside your loop, then display a title only when the date changes.
$date = "";
while ($news = mysql_fetch_array($query)) {
if ($news['datetime'] != $date) {
$date = $news['datetime'];
echo $date;
}
echo $news['title'];
}
Something like this may work for you:
$query = mysql_query("SELECT * FROM `gb_news` order by `datetime` DESC");
$prevDate ='';
while($news = mysql_fetch_array($query))
{
$tmpDate = DateTime::createFromFormat('Y-m-d H:i:s', $news['datetime']);
if($prevDate!=$tmpDate)
{
$prevDate = $tmpDate->format('d/m/Y');
echo '<h1>'.$prevDate.'</h1><hr />';
}
echo '<p>'.$tmpDate->format('H:i').' - '.$news['title'].'</p>';
}
This is assuming your datetime column is in the format YYYY-MM-DD hh:mm:ss and you want to display the date as DD/MM/YYYY, obviously edit to taste!
Also your PHP version must be >= 5.3.0 to use the DateTime class
Well what you can do is store the date and then change the layout each time the date changes:
while($news = mysql_fetch_array($query)) {
if(empty($date) || $date != date("format", $news['datetime'])){
$date = date("format", $news['datetime']);
//echo/print some html to show new section
}
echo $news['title'];
echo $news['datetime'];
}
This will handle the date comparisons and knowing when to change dates. "format" indicates the kind of date formatting you would like since you can echo/print the $date.
PHP date
NOTICE: Do not use MySQL_* for it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO instead
I'm not sure I quite understand what you're trying to accomplish. To get your data to display like the image you included you would want to group items of the same date either in MySQL or PHP (or combination really).
Something like:
while ($news...) {
// Get this article date
$thisDate = new DateTime($news['datetime']) // Assuming this is a true datetime value
// Compare this article date with previous article date to determine if we should start a new date header
if (!empty($onDate) // We have previous articles to compare with
&& ($thisDate->format('l d F') == $onDate)) // This article date matches previous so keep in same group
{
echo $news['title'];
} else { // No existing articles or new date = new header
$onDate = $thisDate->format('l d F'); // See options: http://www.php.net/manual/en/function.date.php
echo $onDate
echo $news['title'];
}
}
Got stuck in a complex or maybe stupid problem. I am getting a query from mysql, and then trying to compare a date column with a PHP data which i formatted to the same format i.e "Y-m-d" it always results in no match, although i see there is a match.. and it gets the right result set too.
<?php
date_default_timezone_set('America/Los_Angeles'); // set timezone to our timezone
$constantTime = time(); // get value of time in constant
$appDate = date("Y-m-d", $constantTime); //that defines php time variable -
$queryDate = "SELECT * FROM date WHERE date='$appDate'";
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resulDate);
if ($appDate == date("Y-m-d", strtotime($recordDate['date']))) {
echo "MATCH ";
$dateID = $recordDate['dateID'];
} else {
mysql_query("insert into date(date) values('$appDate')")or die("Database write error1");
$resultDate = mysql_query($queryDate) or die("Sorry Website Under Maintainence");
$recordDate = mysql_fetch_array($resultDate);
echo "NO MATCH ";
$dateID = $recordDate['dateID'];
}
This is always triggering the else, i tried === instead of ==, i tried strcmp
As i assume you're comparing datetime field, you have two possibilities:
Cast field to date:
$queryDate = "SELECT * FROM your_table WHERE date(your_date_field) = date('$appDate')";
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date
or
Modify your date format to be ISO compatible:
$appDate = date("Y-m-d H:i:s", $constantTime); //it defines date in format 2015-03-14 15:00:00
$queryDate = "SELECT * FROM your_table WHERE your_date_field='$appDate'";
See also this question
I've looked at several of the threads that have to do with this issue and still can't figure out what's going wrong with my code.
I'm bring a date field from my form that is in this format "mm-dd-yyyy", when I bring it to my code to update the date of birth field as "yyyy-dd-mm" I use the following
$birth=$_POST['dateBorn'];
$dateB = date('Y-d-m',strtotime($birth));
$finaldateB = ($dateB === false) ? '0000-00-00' : date('Y-d-m',strtotime($dateB));
When I echo the values of the variables (using a date of birth of 11-23-2012) I see the following values for them birth = 11-23-2012, dateB = 1969-31-12, finaldateB = 1969-31-12
I'm obviously doing something incorrect and haven't been able to locate where
TIA
In order to get things to function, I've at least discovered a work-around that may not be elegant but gives me the results I need for now
$dateB = $birth;
$dateborn_a=explode("-",$birth);
$yearborn = $dateborn_a[2];
$dayborn = $dateborn_a[1];
$monthborn = $dateborn_a[0];
$dateOfBirth=$yearborn."-".$monthborn."-".$dayborn;
And then use the value in $dateOfBirth to update the table. It's worked for all the records I've tested it on so far.
$dateB = date('Y-d-m',strtotime($birth));
here
y is for year
d is for day
m is for month
so formate will be yyyy-dd-mm
try
$dateB = date('Y-m-d',strtotime($birth));
You're using Y-d-m instead of Y-m-d. MySQL's date format should be YEAR-MONTH-DAY. Using
$dateB = date('Y-m-d',strtotime($birth));
should fix it.