find difference in seconds [duplicate] - php

This question already has an answer here:
Difference between 2 dates in seconds [duplicate]
(1 answer)
Closed 8 years ago.
date_default_timezone_set('America/New_York');
$search_date = '2012-12-19 13:22:00';
$right_now = date('Y-m-d H:i:s');
$search_date = new DateTime($search_date);
$right_now = new DateTime($right_now);
$interval = $search_date->diff($right_now);
echo $interval->format('%R%s seconds');
This displays how many seconds are different between the search date and right now.
I would expect it to return more than a two digit value because there is more than a 99 second difference between the two dates, so I am not sure what I am doing wrong.

Alternatively, with very little change to your original code:-
date_default_timezone_set('America/New_York');
$search_date = new DateTime('2012-12-19 13:22:00');
$right_now = new DateTime();
$seconds = $right_now->getTimestamp() - $search_date->getTimestamp();

Related

How can I calcualte Age in Years, Months and Days [duplicate]

This question already has answers here:
Calculate months, years and days between two given dates as timestamp [duplicate]
(4 answers)
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 2 years ago.
Here is my code:
<?php
$userDob = $result['DoB'];
//Create a DateTime object using the user's date of birth.
$dob = new DateTime($userDob);
//We need to compare the user's date of birth with today's date.
$now = new DateTime();
//Calculate the time difference between the two dates.
$difference = $now->diff($dob);
//Get the difference in years, as we are looking for the user's age.
$age = $difference->y;
//Print it out.
echo $age;
?>
I want to output something like 50 Years, 6 Months, 20 Days but I don't know how to exactly put it as this method is only outputting number of years only.
Yes php has something built in for that :)
$age_check = "$day-$month-$year";
$today = date("Y-m-d");
$calculate = date_diff(date_create($age_check),date_create($today));
$age = $calculate->format('%y');

How can I convert current date and time in special time zone to decimal(16,4) in PHP [duplicate]

This question already has answers here:
DateTime with microseconds
(10 answers)
Closed 4 years ago.
I want to convert the current date and time with special timezone to decimal(16,4) in PHP.
That's my code:
$gmtTimezone = new DateTimeZone('Europe/London');
$myDateTime = new DateTime("now" , $gmtTimezone);
$time = $myDateTime->format('U');
but It returns a decimal(16) number in GMT and shows 1549351821 but I want something like that:
1549351821.1589
use this code:
$gmtTimezone = new DateTimeZone('Europe/London');
$myDateTime = new DateTime('now', $gmtTimezone);
$time = $myDateTime->format('U');
$formatted_num = number_format($time, 4);
echo $formatted_num;
output: 1,549,355,258.0000;

Php subtract two dates [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I am getting $status_date from mysql database and when I echo $status_date it looks like this -> 2016-01-08 22:18:14. I am setting $now and when I echo it looks like this -> 2016-01-08 22:43:27. I want to subtract $status_date from $now. Here is my code:
$now = date("Y-m-d H:i:s");
$status_date = date("Y-m-d H:i:s",strtotime($row['status_date']));
$x = $now-$status_date;
$x returns this -> 0
Why is it not subracting?
Dates don't subtract like that. Convert both dates to time, subtract, and convert back to a date.
$now = strtotime(date("Y-m-d H:i:s"));
$status_date = strtotime($row['status_date']);
$x = date($now-$status_date);
However if you're trying to get the time between the dates consider the moment library.
https://github.com/fightbulc/moment.php
You can also use something like:
$now = new DateTime();
$status_date = new DateTime($row['status_date']);
$diff = $now->diff($status_date);
Then you can use any of the dateInterval properties or functions with $diff.
Documentation: http://php.net/manual/es/datetime.diff.php
http://php.net/manual/es/class.dateinterval.php
according to http://php.net/manual/en/datetime.diff.php
check this code i wrote here :
https://eval.in/499594
<?php
$strStart = '2015-01-08 22:18:14'; // status date
$strEnd = 'now';
$dteStart = new DateTime($strStart);
$dteEnd = new DateTime($strEnd);
$dteDiff = $dteStart->diff($dteEnd);
print $dteDiff->format("%Y-%m-%d %H:%I:%S");
thre result is smth like : 01-0-0 00:38:42

Adding different times in php [duplicate]

This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
Hello just a simple question here.
Php's time related functions are confusing me ;-(
Given 2 variables
$start = "2013-07-25 20:24:13" ('Y-m-d H:i:s');
$duration = "0:55" ('H:i');
How may I add $duration to $start? should result in:
"2013-07-25 21:19:13"
Use DateTime() with DatePeriod()
$dt = new DateTime('2013-07-25 20:24:13');
$dt->add(new DatePeriod('P55M'));
echo $dt->format('Y-m-d H:i:s');

Weeks between 2 mysql formatted datetime [duplicate]

This question already has answers here:
MySQL week calculation between two dates
(2 answers)
Closed 10 years ago.
I am creating a Top 30 Music Chart..
Hw do I get the number of weeks between two mysql formatted datetimes? like
From: 2013-01-15 11:41:14
Current Datetime: 2013-02-25 13:41:14
Using SQL is better, but in php it'll be something like this:
<?php
$datetime1 = new DateTime('2013-01-15 11:41:14');
$datetime2 = new DateTime('2013-02-25 13:41:14');
$interval = $datetime1->diff($datetime2);
$diff = $interval->format('%d');
echo (int)$diff/7;
Subtract the times using strtotime
$difference = strtotime($first_date)-strtotime($second_date);
$weeks = round($difference / 604800 );
<?php
$db_time_a = strtotime('2013-01-15 11:41:14');
$db_time_b = strtotime('2013-02-25 13:41:14');
$seconds_in_between = $db_time_a - $db_time_b;
$hours = (int)($seconds_in_between/60/60);
$minutes = (int)($seconds_in_between/60)-$hours*60;
$seconds = (int)$seconds_in_between-$hours*60*60-$minutes*60;
echo 'The time in seconds in between the two times is: '.$seconds_in_between.' (Hours:'.$hours.', Minutes:'.$minutes.', Seconds:'.$seconds.')';
?>
You can just chunk down the seconds with "remainder" and find out the minutes (and it's remaining seconds) and so on until you hit the time-distance you want.
does the WEEK() function helps you in your query ?
I don't know Mysql very well but lets assume the following :
select WEEK(date1-date2) from mytable where name='Ken';
You can use strtotime() method, then count difference between current time and time you have in your MySQL database.
Try DATEDIFF(date1, date2) function in mysql.
DATEDIFF() will return you the no of date between two given date. Hope you can easily get the weeks from output.

Categories