Why my comparison of two dates in PHP is not working? - php

I have removed the desired output and some styling as they dont affect the question. I cant seem to compare the two dates properly, idea is that if currentDate is greater than deadlineDate there will be no output for that route. What i am trying to do is prevent the system from listing routes which are already closed. I dont understand why its so difficult or then i am missing something very basic here.
<?php
$driveDays = mysql_query("SELECT date,routeid from StopDates where routeid='".$row['id']."' ORDER BY date ASC");
while($stopDates = mysql_fetch_array($driveDays)){
$orderDaysBefore = $row['lastOrderDate']; // How many days before the order must be placed.
// Change the date taken from the query to new format
$originalDate=($stopDates['date']);
$newDate = date("d.m", strtotime($originalDate));
// Count the deadline date for the route.
$deadlineDate = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
$deadlineDate = Date('d.m.y G:i', $deadlineDate);
//Get current date which is then compared to the deadline date. Idea is that if currentDate is larger than deadlinedate there will be no input.
$currentDate=Date("d.m.y G:i");
//The line below doesnt seem to be working, i have tried mktime and time too but for some reason it just cant compare.
if (strtotime($currentDate) > strtotime($deadlineDate)){
// Output nothing
}
else { ?>
<p>Output stuff here</p>
<?php
}
}
?>
Problem is that for some rows it hides the route and for some it doesnt. I have tried to do this with mktime and time but i cant seem to figure out what the problem is. Most of the guides i see tell to convert dates to unix timestamp format and if i understand correctly thats exactly what i am trying to do here. Im pretty sure my mistake is a simple one.
Strange thing is that for some dates it seems to work like if deadlineDate is over a month old. deadlineDate forms correctly in the d.m.y G:i format.
Solved
I skipped formatting and compared strtotimes
I added:
$currentDate2=strtotime("now");
$deadlineDate2 = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
And then i compare $currentDate2 and $deadlineDate2

Skip the formatting and just compare the results from strtotime().

Related

Show text after a date has passed

I am trying to get a simple line of text to appear if todays date is after another date.
I can either get it to appear on all pages or none, but I am unable to get it to display based on whether the challenge start date is before or after todays date. I believe it could be a date format issue, but everything I have tried has fallen short.
Here is my code:
Get todays date
$date_now = new dateTime();
Challenge start date
$challengeStartDate = date('dS F Y', strtotime($this->item->start_date));
echo '<!--' . strtotime('1970/1/1 00:00:00 +' . $validity) . '-->';
New text line
if ($challengeStartDate > $date_now) echo "New Text";
date() returns a string. With $challengeStartDate > $date_now it's like comparing if one string is bigger than the other (not sure if your dateTime handles that).
Your approach is otherwise fine. Just use timestamps to compare. time() gets you the time as a Unix timestamp:
$now = time();
if ($now > strtotime($this->item->start_date)) {
// do your thing
}
Something like this is more what you need. Try it out.
I had the very same problem some time ago.
All you need to do is store your local time in a database so it would be saved statically.
Because in your example, both $challengeStartDate and $date_now will change and update simultaneously and you wiill always get the current pc time!
Try storing it in a table or idk maybe sessions would help too.

Formatting error after finding the difference in time with php?

I am looking to find the minutes between two times with php. I have followed the answers I found from other questions on Stack and researched the PHP manual. In my situation, I am getting a DATETIME that is stored in the database and comparing it to NOW. I think my issue lies with a formatting issue. I will post what I have put together.
$order_start = $obj->order_start; //getting detetime from database query
$order_start = DateTime::createFromFormat('Y-m-d h:i:s', $order_start);
$order_start= $order_start->format('Y-m-d h:i:s'); //format detetime
$now_date = date('Y-m-d h:i:s'); //get now time
$diff=date_diff($order_time,$now_date); //get difference
$diff=$diff->format("%i minutes"); //format difference
So whats going on here. The page stops working when I use this line, $diff=$diff->format("%i minutes");
I was under the impression I had to format the date from the database by using, $order_start = DateTime::createFromFormat('Y-m-d h:i:s', $order_start);.
$order_start echo's 2017-07-31 01:44:25
$now_date echo's 2017-07-31 04:43:59
Which looks like the same format but may not be?
Now I'm trying to get the difference between these times and format the result to show minutes.
Thanks for the advice in advance! If there is a better article on here let me know.
I tried
Echo Difference in Two Times
I also read this to try and figure out the formatting.
http://php.net/manual/en/dateinterval.format.php
My Order page
You may convert your dates into timestamp and get the difference, then format it accordingly. Use like this
echo date('H:i:s',(strtotime($now_date)-strtotime($order_start)));
in your case use like this
echo date('h:i:s',(strtotime("2017-07-31 01:44:25")-strtotime("2017-07-31 04:43:59")));

Unable to understand time() and date() functions

I am trying to get current time and date in order to echo it out on my website. I have the follow snippet:
$date_of_msg = date("Y-m-d");
$time_of_msg = time();
When I echo $time_of_msg I get 00:00:00. I have tried to edit my code based on this solution here but which this approach, when I echo the variable, I get 838:59:59. I simply want the current time to be displayed in 24 hour format.
In addition to this, I currently have the date formatted to (Y-m-d), which is great because it works. I am trying to format it so that it displays day, number, year, i.e. today is 20th Feb, so I want the date to display Feb 20, 2016. I have tried the following based on documentation (see here)
$date_of_msg = date("F j, Y")
But again, the date displays nothing. Am I missing something?
If you need the current time in the 24h format just use
$time_of_msg = date("H:i");
The date part seems correct that way, you must be doing something wrong while displaying it.
time() (unless you override it in some weird fashion) gives you a timestamp, i.e. the amount of seconds which have passed since 1970-01-01 until now. date(), however, gives you a string representation of a date, which may or may not include the minutes and seconds - depending on how you choose to format it.
So, if you want to display the time and date to a user, you should probably go for something like
$date_of_msg = date("F j, Y H:i:s")
The documentation on date() gives you an excellent description of available options.
time() returns a UNIX timestamp while date() format a timestamp. Your call date("Y-m-d") means the same as date("Y-m-d", time()).
Even though the function is called date(), it can also format time. You just have to use the correct placeholders. E.g. date("H:i:s") would give you a 24h-time like 17:43:23.
This code
<?php
echo date("Y-m-d").PHP_EOL;
echo time().PHP_EOL;
echo date("F j, Y").PHP_EOL;
Returns this result, as expected
2016-02-20
1455927480
February 20, 2016
So what are you doing that you are not actually telling us

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.

Make strtotime() generate times only in the future

I'm trying to make a PHP script to find the next occurence of a date (say 5-1). However, if I put that into strtotime() on 5-2 it comes up with the date from this year. I simply want to make it always return the next date possible. How can I make this work? I'm using codeigniter if that helps.
EDIT: Here's some code I came up with, if some humble soul runs across the same problem:
if (strtotime($date) < strtotime("now")) {
$realseconds = strtotime($date) + 31556926; //add one year in seconds
} else {
$realseconds = strtotime($date);
}
You could check whether the date returned is earlier than the current time, and, if it is, add one year to it.
You could also pass some date in the next year as the second parameter to strtotime.
What is 5-1 or 5-2?!
Try doing this instead:
strtotime('next October');
Assuming 5-2 means february fifth, you could do
strtotime("february 5 +1 year")
Code:
$x=strtotime($inputString);
$YEAR=60*60*24*30*12;
while($x<$time()) $x+=$YEAR;
Basically, it adds one year if the date returned by strtotime is in the past... because i used while() it will never return a date in tha past even if it was explicitly stated like that

Categories