Solved

PHP - Check if string of date/time is less than 24 hours ago.

Posted on 2014-01-10
4
2,808 Views
Last Modified: 2014-01-10
Hi,

How do I check if $input is less than 24 hours ago? I cant seem to get the conversions right.
$input = "10/01/2014 07:28:32";

Open in new window


I tried this but it didnt work -
if(strtotime($input) < (time() - (3600*24)))

Open in new window


Thank you
0
Comment
Question by:mhdi
4 Comments
 
LVL 13

Accepted Solution

by:
stergium earned 167 total points
ID: 39770413
try this

$date = '10/01/2014 07:28:32';
if (strtotime("$date +1 day") <= time()) {
    // Do something
}

Open in new window

0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 167 total points
ID: 39770417
You should read this page http://us3.php.net/manual/en/function.strtotime.php and the page about Date and Time Formats which will tell you what is acceptable.  Using slashes can be a problem too as is mentioned on that page.
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 166 total points
ID: 39770420
if the error/issue is that the strtotime does not properly recognize the date/time, you need to use another function:
http://be2.php.net/manual/en/function.date-parse-from-format.php
0
 

Author Comment

by:mhdi
ID: 39770477
Thanks for the comments. Those links where very helpful.

@stergium I was very confused for a long time, but it seems your code works if I reverse the <= it should be >=
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now