PHP unix timestamp compare [duplicate] - php

This question already has answers here:
Does PHP time() return a GMT/UTC Timestamp?
(5 answers)
Closed 7 years ago.
I have an event date & time stored in my database that is being saved as a PHP unix timestamp. Im trying to check if today is greater than the event date and time.
The problem is - i'm trying to check it using local time (America/New_York), but im getting 2 different time zones. time() is displaying in EST and my database is displaying in UTC
Is there any way to check it correctly?
My Event Date from database:
1450447200 (December 18, 2015 2:00pm)
Im trying to compare it with php time()
am I maybe doing this wrong?
*UPDATE - ANSWER*
as per this answer I ended up doing this:
$utc_str = gmdate("M d Y H:i:s", time());
$today = strtotime($utc_str);
$event_datetime = $Event_timestamp;
date_default_timezone_set("America/New_York");
$utc_str_event = gmdate("M d Y H:i:s", $event_datetime);
$event_date = strtotime($utc_str_event);
if($today >= $event_date){
//Do Something
}

You probably have wrong date on server(s). time() always should return epoch (epoch = UTC).
btw. saving date as int on database it isn't best practice

I don't know if this is correct but have you tried using date_default_timezone_set

Related

How to get next hour's 15th minute of time from given time in PHP [duplicate]

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)));

php: how to add minutes to timestamp [duplicate]

This question already has answers here:
PHP: add seconds to a date
(9 answers)
Closed 8 years ago.
I am trying to figure out how to easily add minutes to a timestamp in php.
I can do this easily in SQL such as select now() + interval '60 minutes' or using the DATEADD function, however I am not sure how to easily do this in PHP.
I have a date variable that I call as:
$date = date("Y-m-d H:i:s");
And now I just need to add or subtract time from this.
Can anybody help?
Thanks!
You could use strtotime() like this:
$date = date('Y-m-d H:i:s', strtotime('now +60 minutes'));
Which would give you the date 60 minutes in the future formatted the way you'd like.
If you have the timestamp is like this
$tmt += 2 * 60 * 1000;
$date= date("Y-m-d H:i:s", $tmt);
Try this

handling time format in mysql and php [duplicate]

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

Addition of time difference of time zone (to GMT value) [duplicate]

This question already has answers here:
Display time/date in specific timezone using date() function
(2 answers)
Closed 9 years ago.
The database stores values as YYYY-MM-DD HH:MM:SS (eg 2013-04-14%2015:00:00) which is in GMT time but the true value is the the above date and time + time zone difference of the server.
$time_heard = '2013-04-14%2015:00:00';
$time_diff = date('p')
$real_time = $time_heard + $time_diff ;
echo $real_time ;
How can i do this addition.
Thanks
If you're using MySQL you can use the CONVERT_TZ functionality straight in the SELECT.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz
If you need to do it in PHP, use the DateTime class by creating a new DateTime with the timezone set to GMT then changing the timezone and when you output the date it should display as the new timezone.
you can use date_default_timezone_set() function to set the time to the zone you want
here you can find different zones: http://www.php.net/manual/en/timezones.php
or if you insist, you can use date_parse_from_format()
here is the manual for it: http://php.net/manual/en/function.date-parse-from-format.php
edit: another way to do it with easy functions:
$tmstamp = strtotime($date1) + strtotime($date2);
echo date("YYYY-MM-DD HH:MM:SS", $tmstamp) ;

how to convert time() output to a date in php [duplicate]

This question already has an answer here:
Converting facebook time to human readable time with PHP
(1 answer)
Closed 9 years ago.
I am using time() command in php to store a timestamp value for every time database is updated i.e whenever a value is updated in database a timestamp is added in a time.
For eg: when a change was made to database yesterday night, the value added was 1368132319.
I know the the time() commands returns the no of seconds elapsed from jan 1 1970.
Now what i want to do is convert these no of seconds into a user understandable form which can be displayed on an html page. Like these seconds are converted to date and time.
How do i do that? i cannot think of a logic to implement it. googled it but to no avail
Pretty simple thing, simply use datetime
$date = date_create();
date_timestamp_set($date, 1171502725);
echo date_format($date, 'U = Y-m-d H:i:s') . "\n";
All in the manual http://php.net/manual/en/datetime.settimestamp.php
you can see the demo link....
<?php
$time_in_seconds = 1368132319;
$format = 'Y-m-d H:i:s';
echo date($format,$time_in_seconds);
?>
You can use the date() function. Example:
$timestamp = 1368132319; // in your case the value from the DB
echo date('Y-m-d', $timestamp); // 2013-05-09
echo date('Y-m-d H:i:s', $timestamp); // 2013-05-09 23:45:19
simply you can use below
<?
echo date("D-M-Y",$strtime);
?>
there are various format available for DATE function in PHP you can use required one.

Categories