I'm in a problem that i need an help because after research i have not found a solution.. I have to compare two date in PHP, the date of now with the date from Database.
Writing this:
strtotime(Date("d/m/Y H:i"))
Correct return the current timestamp.
But writing this:
strtotime($m['start_date'])
Work but response with a timestamp less than the current timestamp but the $m['start_date'] is from Database and it's like this: "2017-08-23 11:00:00"... It's not possible that the timestamp of 2017-08-23 11:00:00 it's less than the current timestamp!
And the weird part is that if i write this:
Date("d/m/Y H:i",strtotime($m['start_date']))
It response with the correct Date formatted with the string i passed to: 23/08/2017 11:00. How it possible? I have to compare the two timestamp but the output is that today come after tomorrow...
I have tested a lot of solution but not working at all. I have tried also to write this:
strtotime(Date("d/m/Y H:i",strtotime($m['start_date'])))
But not return nothing, 0.
I have also tried to put the second, but the result is the same.
I'm falling all for a stupid comparison from two date, help me!
After today i definitely hate work with Date
If you are trying to compare two timestamps from why don't just use the time() function and compare if it's less, greater or equal to the timestamp of the value received from the database by using strtotime($m['start_date']) for example
$db_time = strtotime($m['start_date']);
if ( time() > $db_time ) {
/**
** YOUR CODE
**/
}
Related
I have stored the date as a string in my DB in this format (dd-mm-yyyy).
Here I want to check the difference in days between the current date and the date in DB.
Here is my controller code:
public function index()
{
$domain_count = domain_details::get()->count();
//var_dump($domain_data);
$domain_alert = domain_details::
where('domain_ex_date','>',date('j-m-y'))
->get();
return view('home1')->with('domain_count' , $domain_count)
->with('domain_alert' , $domain_alert);
How do I achieve this? Is my approach right?
The above code shows 2016 is greater than 2017. I can see my logic is wrong but how do I change this?
It's better to have your dates in a DATE column in a proper format, otherwise MySQL won't know how to calculate it. Since you don't, you'll have to convert it with str_to_date, passing in the raw command:
where(DB::raw("str_to_date('domain_ex_date','%d-%m-%Y')"),'>',date('Y-m-d'))
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.
Hi everyone im stuck with this: I want to erase records on a database that are in the past. I just want to get into account Month and Day. For example, if the database record is (this is how is formatted on the DB) "Apr 5 2013" i need to compare it with today's date "Apr 6 2013". In this case, this record gets deleted. I´ve seen examples using UNIX timestamp, but none using that format using the date('M j Y'). Thanks!
The better way to store dates in databases is using DATE or DATETIME format. SQL allows you to get all informations you want from those types. But a request like this should work.
You could do it eventually with a regular expression, but it would be very heavy...
But you can do it in PHP, getting all results, then browsing them and comparing dates using something like strtotime, and then deleting every ID. Still very heavy but easier to implement than within a SQL request. But it still would be better if you could change the SQL architecture.
The approach I would try would be to convert the english text string into a unix timestamp using strtotime() then compare the new timestamp to the current date. Here is some php pseudo-code that describes the approach.
$threshold_to_deletion = numbers of days old a record is allowed to be;
$is_it_yesterday = 0;
$current_date = new DateTime();
$current_date->getTimestamp();
if ( !($is_it_yesterday = strtotime($str_from_db)) === false ) {
$date_interval = date_diff($is_it_yesterday, $current_date);
if( $date_interval >= $threshold_to_deletion ) {
Do stuff here to delete
}
} else {
echo "DB string is not a valid date: $str_from_db";
}
Please note it is not finished or tested, it's just there to loosely describe how it would be done. I hope this helps.
Tim Dumas
www.mpact-media.com
I need to compare two dates to show an edit link if it is within 5 mins after the post was made, in PHP. If more than 5 minutes have passed, don't show anything.
$answer_post_date = get_the_time("Y-m-d");
$current_date = date("Y-m-d");
$formated_current_date = strtotime($answer_post_date);
$formated_answer_post_date = strtotime($current_date);
At this point I have two values:
1274414400 ($formated_current_date)
1276056000 ($formated_answer_post_date)
I am not sure what to do next to check if the current date/time is > 5 mins from the answer post date.
Any suggestions would be great.
All I really need the answer to be is a Boolean (yes/no) and if yes, display the minuets left to show the link to edit.
You're only handling dates, how are you supposed to know if the difference is 5 minutes?
Anyway, I'd say the majority of the PHP code that uses the default PHP functions is at least somewhat broken. The problem is you, despite a unix timestamp storing the correct point in time something happens, it does not store timezone information. See here.
So, forget using only date and strtotime. Use the datetime extension.
Store in the database the Unix timestamp and the timezone (by timezone I mean e.g. Europe/Lisbon). Then:
$tz = new DateTimeZone($timezone);
$answer_post_date = new DateTime("$timestamp");
$answer_post_date->setTimeZone($tz);
$current_date = new DateTime("now", $tz);
$diff = $current_date->diff($answer_post_date);
if ($diff->format("a") > 0 ||
$diff->format("h") > 0 ||
$diff->format("m") >= 5) {
//more than 5 minutes have passed
}
Of course, for comparing dates, you can always compare the timestamps.
My understanding of what you need to do:
$delta = ($formated_current_date - $formated_answer_post_date) / 60; // in minutes
if ($delta < 5) {
// show $delta
}
EDIT: Like others pointed out, this alone will not fix all of the issues at hand. As I see it, the smallest change to your current code would be to use a date format with higher granularity - such as "Y-m-d H:i:s". This being enough, like others pointed out, is contingent on the post's date being in the same timezone as your system.
I don't see the need to do a round-trip to a string format and back, regardless of how efficient or reliable it is.
date() will default to calling time() which you can call directly and get the current time in seconds as a Unix epoch timestamp (which is what you're trying to end up with in $formated_answer_post_date). You need to look in the WordPress docs to find the equivalent based on the post's value.
Then you can do a simple comparison of seconds. 5 minutes is 300 seconds.
You will still need to check that the code can assume the timezones of both values will be the same.
I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";