This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.
Related
This question already has answers here:
Adding minutes to date time in PHP
(13 answers)
Closed 1 year ago.
Is there a way of getting the next hour's 15th minutes from given time?
For Example
if input is 2021-08-26 12:00:37 then output should be 2021-08-26 12:15:00
if input is 2021-08-26 12:30:37 then output should be 2021-08-26 13:15:00
Use Carbon
It is installed by default in Laravel.
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2021-08-26 12:00:37');
$newDate = $date->addMinutes(15)->format('Y-m-d H:i:s');
You can convert the date to a timestamp and then add it the equivalent of 15 minutes in seconds (15 * 60) and then convert it back to a date
I think this is what you looking for...
$start = '2018-05-21 20:24:45';
echo date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($start)));
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);
Check the library here
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}
This question already has answers here:
How to add 5 minutes to current datetime on php < 5.3
(7 answers)
Closed 8 years ago.
I have a mysql table which contains a field named time and has the "time" format. I added the first line into the table manually and its time is "14:55:00".
Now, in my PHP page, users will fill a form and then submit it. Here, I want my php to assign the last register's time value into a variable like $time (i can do that). Then I want this:
$time=$time + 5 minutes
How can I do that?
If you want to update your sql table, you can use the DATE_ADD function:
UPDATE table SET time = DATE_ADD(time, INTERVAL 5 MINUTE)
If you want to add 5 minutes to a timestamp in PHP you can use the function strtotime:
$time = $time + strtotime("+5 minutes");
If your time is a string (ie in format 14:55:00), you can do the following:
$timeAsString = "14:55:00";
$timestamp = strtotime("+5 minutes", strtotime($timeAsString));
$time = date("H:i:s", $timestamp);
echo $time;
$dateMinutes= date("i")+5;
and use the make time function from php
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert Unix timestamp to hhmmss?
I have a timestamp in the format of:
1330559989
How to check the hours in the timestamp is in between 6 hours and 23 hours?
Can someone help me please?
date('H', 1330559989) will give you the hours in 24h format with a leading zero, in this example: 23.
There is also a specific function for the hour of a timestamp called getdate which offers an array interface to that information (demo):
getdate(1330559989)['hours']; # 23
$hrs=date('H', 1330559989);
if ($hrs>= 6 && $hrs<= 23) {
// your code
}
If you want to check the time between now and that given timestamp, then you can use:
<?php
echo date("H", strtotime(now)-1330559989); // Displays the difference from now.
echo date("H", 1330559989); // Displays that time!
?>
Hope this helps! :)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.