I have Id's I went to check if they have expired, they're in y/m/d format so I get todays date in that format and it comes out incorrect.
How can I compare
2021/03/15
With 2021-12-25
The dashes versus slashes doesn't seem to matter, so I think it's the month
Todays date is parsed as a date, the other is a string from an SQL statement array
$utz = new DateTimeZone($_SESSION['timezone']);
$dt = new DateTime(null, $utz);
$today = $dt->format('Y/m/d');
while($myrow = pg_fetch_assoc($result))
if ($myrow['id1_expiry'] < $today) {
printf ("<tr bgcolor=#FD5D5D>.... }else{
You could compare the DateTime objects using comparison operators.
You could use a formated date to create a new DateTime object, for accepted format.
$utz = new DateTimeZone($_SESSION['timezone']);
$date = new DateTime(null, $utz);
while ($myrow = pg_fetch_assoc($result)) {
if (new DateTime($myrow['id1_expiry'], $utz) < $date) {
echo 'expired';
}
}
Related
I have a code in PHP 5.5.11 where I am trying to do the following:
Get today's date in a variable --> $today
Calculate the end of month from a date in a form --> $st_dt_eom
if difference between these 2 dates is more than 5 days then execute a code. The code in the if condition below does not execute.
$today= date();
if($_POST['Submit']=='SAVE')
{
$st_dt=YYYYMMDD($_POST['st_dt'],"-");
$st_dt_eom= datetime::createfromformat('YYYYMMDD',$st_dt);;
$st_dt_eom->modify('last day of this month');
$diff = $today->diff($st_dt_eom);
$diffDays= intval($diff->format("%d")); //to get integer number of days
if($diffDays>5){
redirect("index.php");
}
}
An example:
// 2022-12-19
$today = date('Y-m-d');
// $_POST['st_dt']
$st_dt = '2022-12-31';
function dateDiffDays($today, $st_dt)
{
$today_obj= DateTime::createfromformat('Y-m-d', $today);
$st_dt_eom= DateTime::createfromformat('Y-m-d', $st_dt);
$diff = $today_obj->diff($st_dt_eom);
return $diff->days;
}
// int(12)
$res = dateDiffDays($today, $st_dt);
use var_dump to locate your bug.
A few suggestions to improve your code and produce something workable:
<?php
// Instead of using date(), use a DateTime() then you're comparing two DateTimes later on
$today = new DateTime();
// I'm assuming that your YYYYMMDD function removes "-" from the $_POST['st_dt']
// to provide a date in the format YYYYMMDD (or Ymd in PHP).
// Unfortunately, DateTime doesn't understand that format.
// So I'd change this for keeping Y-m-d (YYYY-MM-DD).
// Or modify your code to return that format!
$st_dt = $_POST['st_dt'];
// Watch out for your cAsE when using PHP functions!
$st_dt_eom = DateTime::createFromFormat('Y-m-d',$st_dt);
$st_dt_eom->modify('last day of this month');
$diff = $today->diff($st_dt_eom);
$diffDays= intval($diff->format("%d"));
if($diffDays > 5){
redirect("index.php");
}
I'm getting a issue where I can't compare two dates in the format dd/mm/yyyy to check if a date is passed or not. code:
$Today = date('d/m/Y');
$fakturaDate = DateTime::createFromFormat('Ymd', $retrieved_DPFDT3);
if ($fakturaDate < $Today ) {
$x+= $y;
}
I don't know if the format is the problem, but if I could use the current it you be much easier.
Your problem is in that you are comparing string date('d/m/Y') with DateTime object.
Just use DateTime for both dates (-;
$Today = new \DateTime();
$fakturaDate = DateTime::createFromFormat('Ymd', $retrieved_DPFDT3);
if ($fakturaDate < $Today ) {
$x+= $y;
}
Compare dates using php function
if(strtotime($fakturaDate) < strtotime($Today))
Try searching on how to compare dates in php.
I've created an If an Else Statement but it doesn't work properly.
I have some dates within my SQL which have been retrieved and stored in variables using PHP.
I'm comparing the current date with the dates from the database but for some reason, it thinks for example that 29-09-2015 if LESS THAN 31-01-2015.
I can understand that the format could be the issue d,m,Y but I thought I'd corrected that already.
Here's the code:
$today = date('d-m-Y');
$date = $row['respondby'];
$euDate= date("d-m-Y", strtotime($date));
<?php
if($today < $euDate){ echo "<td>". $today." is less than ". $euDate ."</td>";
}
else{
echo"<td>Lost somewhere in between ?!?!?! :S </td>";
}
?>
As a result it prints
29-09-2015 is less than 30-06-2015
today's date was 29-09-2015 and one of the dates was in the data was this one as shown.
Thank you everyone that helps.
Comparison of dates as strings uses lexicographical order, hence your result is "correct".
Instead of d-m-Y format, try to use Y-m-d, this guarantees proper ordering.
$today = date('Y-m-d');
$date = $row['respondby'];
$euDate= date("Y-m-d", strtotime($date));
if($today < $euDate) { [...] }
Or, you can use Date objects instead:
$today = new Date('now');
$euDate= new Date($row['respondby']);
if($today < $euDate) { [...] }
I want to separate Date and time from retrieved timestamp in oracle with php but it's not working. I tried to separate date and working after that i have to separate time but i am getting empty timestamp.
$db = $row['CREATEDATE']; // CREATEDATE=03-SEP-15 12.55.17.000000 PM
echo 'db...'.(string)$db."<br/>";
$timestamp = strtotime((string)$db);
echo 'timestamp...'.$timestamp."<br/>";
echo date("m-d-Y", $timestamp);
Convert your received column data to a string, and convert it to a datetime. Then you can do all other formatting on it.
$db = $row['CREATEDATE']; //$db need to be converted to string type
$datetime = new DateTime((string)$db); //I expect (string)$db will considered as a string
$date = $datetime->format('Y M d'); //convert to date
$time = $datetime->format('H:i:s'); //convert to time
$select_stmt = "select to_char(ENDTIME,' hh24:mi:ss') AS CHENG,to_char(RESERVETIME,' hh24:mi:ss') AS CHENG2 FROM RESERVATION WHERE RESERVATIONSTATUS='RENTING' AND RESERVATIONFACILITY = '".$_SESSION['cno']."'";
$stid = oci_parse($con, $select_stmt);
oci_execute($stid);
while ($row = oci_fetch_assoc($stid))
{
$datt=$row["CHENG"];
$datt2=$row["CHENG2"];
}
I have a MySQL database with a list of dates. I want to output all these dates, provided they occur after today, into a page. The dates are stored in the database in DATE format, as Y-m-d.
I've got the following code (excluding the query etc):
$dateToday = date('Y-m-d');
do{
$dateCompare = new DateTime($row['date']);
if ($dateCompare > $dateToday){
echo '<p>'.$dateCompare -> format('Y-m-d').'</p>';
} else {
echo '<p>FALSE</p>';
}
}while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
But this just outputs all the dates, including one I have set in the past for testing purposes. What am I doing wrong?
$dateToday is a string. $dateCompare is a DateTime object.
You should use strtotime() function.
http://www.w3schools.com/php/func_date_strtotime.asp
Try and convert the date from myssql to a datetime object and output.
$changetime = new DateTime($time, new DateTimeZone('UTC'));
ECHO $changetime->format('m/d/y h:i a');
// list of timezones http://us1.php.net/manual/en/timezones.php
I actually use this to output all my MYSQL datetime data - allows me to convert to any timezone. Note, this will assume your datetime is in UTC - you should convert to your timezone.
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row) {
$time = strtotime($row['date']);
if ($_SERVER['REQUEST_TIME'] - $time) {
echo '<p>'. date('Y-m-d', $time) . '</p>';
} else {
echo '<p>FALSE</p>';
}
}