I'm using DateTime to compare Timestamps. $firstDate below is 6 seconds more than $secondDate so I would like $interval to echo - 00:06. If it was the opposite then I would like + 00:06. No change should just display 00:00. Is there a way with DateTime to display the +/- change?
$firstdate = '00:00:06';
$seconddate = '00:00:12';
$one = new DateTime($firstdate);
$two = new DateTime($seconddate);
$interval = $two->diff($one);
if ($firstdate > $seconddate){
echo "-".$interval->format('%I:%S');
}
elseif($firstdate > $seconddate){
echo "+".$interval->format('%I:%S');
}
else{
echo $interval->format('%I:%S');
}
The current code displays 00:06 instead of +00:06 in this example, but if $firstdate and $seconddate are reversed it does echo -00:06 as intended
Just use if else:
if ($firstdate > $seconddate){
echo "-".$interval->format('%I:%S');
}
elseif($firstdate < $seconddate){
echo "+".$interval->format('%I:%S');
}
else{
echo $interval->format('%I:%S');
}
Related
Can someone give me a working example of what my code should be as my efforts are getting me nowhere?
My code currently says have
if (isset($_POST['startdate'])) {
$startdate = DatePickerToTime($_POST['startdate']);
} else {
$startdate = strtotime('today midnight');
}
and
if (isset($_POST['enddate'])) {
$enddate = DatePickerToTime($_POST['enddate']);
} else {
$enddate = time();
}
which works.
However, I want to say if startdate is equal to enddate then add 23 hours and 59 minutes to enddate
I have read the documentation and am trying
if($_POST['$startdate'] == $_POST['$enddate']) {
$_POST['$enddate'] += date("h:i:sa", "23:59:59");
}
but this doesn't work.
$datetime1 = date('2009-10-11 12:30:00');
$datetime2 = date('2009-10-11 12:30:00');
echo "<pre>";
if($datetime1 == $datetime2){
echo "yes";
echo "<br>";
$datetime2 = date("Y-m-d H:i:s", strtotime("+23 hours 59 minutes",strtotime($datetime2)));
echo $datetime1."===================".$datetime2;
}
else {
echo "NO<br>";
echo $datetime1."===================".$datetime2;
}
i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
its not working, not giving the number of days between given two dates.
Try this. It is very simple.
<?php
$date1 = strtotime("2015-11-16 10:01:13");
$date2 = strtotime("2015-05-06 09:47:16");
$datediff = $date1 - $date2;
echo floor($datediff/(60*60*24))." days"; //output 194 days
?>
It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question
Try this,
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$datediff = $pur_dt - $todate;
$diff = $datediff/(60*60*24);
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try This :
$pur_dt = Date('2015-08-03');
$todate = Date(date('Y-m-d'));
$pur_dt = strtotime($pur_dt);
$todate = strtotime($todate);
$seconds_diff = $todate - $pur_dt;
$$diff = floor($seconds_diff/(60*60*24));
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try this
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff->days>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
I have a timestamp in '2013-04-10T09:00:00Z' format and based on this I want to check the requested datetime format is within 10 minutes or not.
Try this:
$now = time();
$timestamp = strtotime('2013/04/08T09:00:00Z');
$timediff = $now - $timestamp;
if (floor($timediff/60) > 10) {
echo 'Time is 10 minutes older';
}
else {
echo 'Time is not older then 10 min';
}
Below is the code that I tried and it worked for me,
$currentDateTime =new DateTime();
$currentDateTime->setTimezone(new DateTimeZone('UTC'));
$userDateTime = new DateTime($timestamp); //convert users incoming
$diff = $now->diff($then);
$minutes = ($diff->format('%a')*1440)+($diff->format('%h')*60)+ ($diff->format('%i'));
if($minutes <=10)
{
//your code goes here
}
else
{
//time difference is more then 10 minutes<br/>
}
Thanks Francisco for all your post.
is it possible to do something like this.
i was trying to do it but couldn't...
this is what i was trying to do
$date2second = strtotime('2013-03-5');
$date1week = strtotime('2013-03-5') + 604800;
//passed less than
//$datetillnextweek = strtotime('2013-03-5') + 1209600;
$datetillnextweek = strtotime(date('Y-m-d')) + 1209600;
echo "$date2second <br>";
echo "$date1week <br>";
echo "$datetillnextweek <br>";
if($date2second < $date1week && $date2second <= $datetillnextweek)
{
echo "action";
}
$now = new DateTime('2013-03-28');
$one_week = new DateTime();
$one_week->modify('+1 week');
$two_weeks = new DateTime();
$two_weeks->modify('+2 weeks');
if ($now > $one_week && $now < $two_weeks)
{
// you're here
}
See it in action
check the date add function and other date class options. you can specify the interval and the format to present it .
http://www.php.net/manual/en/datetime.add.php
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7D'));
echo $date->format('Y-m-d') . "\n";
?>
To see if its in a time period you can also use http://www.php.net/manual/en/datetime.diff.php to see if the difference iis larger then 7 days
I am storing the date of a post in mysql database in this form y/m/d I want to check if the post is older than 3 days. Is there any way I can convert it to seconds ? and then check like so:
if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}
Using a mix of date() and strtotime() :
$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));
if ($myDate > $oldDate) {
echo "old post";
} else {
echo "new post";
}
Try, strtotime, that can parse various time string formats.
You can do following :
$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3, date('Y', $d1));
$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));
if ($d3 > $d2) {
echo 'old post';
}else {
echo 'new post';
}