PHP - Check difference between 2 different time format - php

I'm saving an updated_at time in the database which is being handled by laravel.
I want to check the difference between a field updated_at value and Now time in minutes .
This is how i get current time :
$now = time();
This is my echo results :
UPDATED AT : 2017-09-10 13:14:29
TIME NOW : 1505051247
Any idea how to check the time between these 2 values by minutes.
Thanks

You are using Laravel so use power of Carbon
$updated_at = \Carbon\Carbon::parse('2017-09-10 13:14:29');
$timeNow = \Carbon\Carbon::createFromTimestamp(1505051247); // but you should use \Carbon\Carbon::now()
$diffInMinutes = $updated_at->diffInMinutes($timeNow);
dd($diffInMinutes);

Use Carbon
use Carbon;
$time_now = Carbon::now();
$updated_at = Carbon::parse('2017-09-10 13:14:29');
$differenceInMinutes = $updated_at->diffInMinutes($time_now);
Hope it's helpful.

Related

how to convert Integer values into timestamp format?

I have an Integer column "duration_temp" that have values represent the duration in minutes, I want to copy those values in another column "duration" of type timestamp, I'm having the problem of how to convert those Int minutes into timestamps format, for example:
if a value in Int is set to 4 then I should convert it to yyyy-mm-dd 00:04:00.
is there a function that can do that or close from doing that?any suggestion would be appreciate it.
If you have a duration in minutes. You could use DateInterval like this.
$yourDate = new DateTime('2021-01-01 00:00:00');
$durationInMinutes = 4;
$interval = new DateInterval("PT{$durationInMinutes}M");
$yourDate->add($interval);
echo $yourDate->format('Y-m-d H:i:s');
https://www.php.net/manual/en/dateinterval.construct.php
If you mean that you just have minutes and want to make a timestamp from it with current date information, try this (after adding use Carbon\Carbon; in top of you file):
$minutes = 4;
return Carbon::create(now()->year, now()->month, now()->day, 0, $minutes)->toDateTimeString();
As your integer column duration_temp is in minutes, you have to convert it to seconds before you can get the expected result.
Take your example :
Int = 4 minutes => Int = 4 * 60 = 240 second
To finish :
date ("Y-m-d H:i:s", 240); // will give you 1970-01-01 00:04:00
DateTime accepts extensive Relative Formats. This makes possible as an example:
$durationInMinutes = 67;
$date = date_create('2021-01-01 '.$durationInMinutes.' Minutes');
//or $date = new DateTime('2021-01-01 '.$durationInMinutes.' Minutes');
echo $date->format('Y-m-d H:i:s');
//2021-01-01 01:07:00
Also works correctly with negative minute numbers.

How to get the current date and time in YYYY-MM-DD HH: MM? LARAVEL 4

I have a table transaction, inside there is column order_on_sale with default value is 0. Then i also have a table config, inside there is column name with two value that is sale_start_date and sale_finish_date. And there are also time columns with values ​​2018-05-1 18:00 and 2018-05-31 18:00 (YYYY-MM-DD HH: mm).
name and time columns contained in the config table are interconnected,
sale_start_date = 2018-05-1 18:00
sale_finish_date = 2018-05-31 18:00
then when someone orders on sale_start_date and sale_finish_date, the order_on_sale column contained in transaction table will change its value to 1
how do i get the current current time (YYYY-MM-DD HH: mm) to make the change?
$transaksi = new Transaction;
$order_on_sale = 0;
if (Config::get('sale_start_date') && Config::get('sale_finish_date')) {
$order_on_sale = 1;
}
$transaksi->order_on_sale = $order_on_sale;
$transaksi->save();
Below is my code, but I am confused how to get the current date and time if I write such code
thanks for the answers you provide
A bit late answer for questioner but maybe can help someone:
$date = Carbon::now();
$formatedDate = $date->format('Y-m-d H:i:s');
echo($formatedDate);
use the Carbon Library which comes by default with laravel
$date = Carbon::now();// will get you the current date, time
dd($date->format("Y-M-D H:m")); //this will dump the date time in the desired format
Maybe with:
use Carbon\Carbon;
$Now = Carbon::now(new \DateTimeZone('My/TimeZoneifRequired'))->toDateTimeString();
So have you tried using Carbon Class ? as far as i know Laravel 4 supports it.
you can get your current date by doing
$now = Carbon::now()->toDateTimeString();
This will retrieve the current date in the following format YYYY-MM-DD HH:mm:ss (as default)
if you want to get the current date in your mentioned format YYYY-MM-DD HH:mm
by wrapping around Carbon and adding some php functionality you can get so :
$currentTime = Carbon::now()->toTimeString();
$now = Carbon::now()->toDateString() .' '. substr($currentTime, 0, strrpos( $currentTime, ':') ) ;
For further explanation go for the docs : https://carbon.nesbot.com/docs/
simply you can use the below code on the view page.
<p>{{ date('Y-m-d') }}</p>
note: HTML <p> tag is optional.
with hours and minutes
<p>{{ date('Y-m-d H:m') }}</p>

PHP MYSQL compare time with created in database

I want to check if 30 min passed after created time in database. created is a time column having time stamp in this format 1374766406
I have tried to check with date('m-d-y H:i, $created) but than of course it is giving human readable output so don't know how to perform check if current time is not reached to 30min of created time.
Something like if(created > 30){}
Try this:
$created = // get value of column by mysql and save it here.
if ($created >= strtotime("-30 minutes")) {
// its over 30 minutes old
}
The better approach is to use DateTime for (PHP 5 >= 5.3.0)
$datenow = new DateTime();
$datenow->getTimestamp();
$datedb = new DateTime();
$datedb->setTimestamp(1374766406);
$interval = $datenow->diff($datedb);
$minutes = $interval->format('%i');
$minutes will give you the difference in minutes, check here for more
http://in3.php.net/manual/en/datetime.diff.php
Here is the working code
http://phpfiddle.org/main/code/jxv-eyg
You need to use strtotime(); to convert the date in human form back to a timestamp, then you can compare.
EDIT: Maybe I misread.
So something like;
if(($epoch_from_db - time()) >= 1800){
//do something
}

timestamp function Pluse 1 hour PHP

I am tring to get a set time timestamp for e.g
if I input the follow time 09:00 i want to see the timestamp for today
function GetTimestamp($time){
date();
}
return GetTimestamp("09:00")
can someone help me please or lead me down the right path
You just have to use the strtotime() function, passing it your time :
$ts = strtotime('09:00');
var_dump($ts);
And you'll get :
int 1300435200
Note : if you don't specify the date, strtotime will use today.
You could also use the DateTime class :
$dt = new DateTime('09:00');
$ts = $dt->format('U');
var_dump($ts);
Use strtotime:
$timestamp = strtotime('09:00');

Check interval between datetimes in PHP

I have a field in database that has type of datetime in which I add time when user visit a page. When user again comes I want to check the interval between his first visit and current. If it is less or equal to 1 hour then I want to show him some message.
I store time like this
2011-03-04 00:25:01
The thing that I want to ask that how to check the interval in PHP
You could try
SELECT COUNT(#UserID) FROM table WHERE LastVisit > (DateADD(now(),interval -1 Hour))
you can then check the count
Edit: added FROM clause
If you have PHP >= 5.3 you can use DateTime objects and functions:
$visit = DateTime::createFromFormat('Y-m-d H:i:s', '2011-03-04 00:25:01');
$now = new DateTime("now");
$diff = $now->diff($visit);
What you can so is, retrieve the the datetime, store it in a variable.
Create a var with time().
You can then convert the db datetime to a timestring using strtotime()
Subtract the datetime timestring from the new time. That should give you a difference in seconds. You can then manipulate your values and do the relevant checks.
$db = datetime_from_database;
$now = time();
$last = strtotime($db);
$diff = $now - $last; //this is in seconds
You can do something like
$minutes = $diff / 60;
If ($minutes > 60) echo 'more than 1 hour; 60 minutes';
Just work in it.
You can then use the date functions to format the new datetime using the $now and update the database.

Categories