How do I compare a time written in this format with the current time to see if this exact moment has happened yet (It's for a countdown timer), but the time must be in this format:
12/31/2012 5:00 AM UTC-0500
Ultimately I want to say if the current time is less than that date, display this, otherwise, display this.
strtotime can convert string dates to unix timestamp which you can easily use later for comparisons/etc
$timestamp = strtotime("12/31/2012 5:00 AM UTC-0500");
if (time() > $timestamp)
...
Related
I currently have a date formated like this:
2017-11-02 11:44:24
However; I need it in this format: 2014-03-11T14:49:52
This is due to the use of a RESTful API based on oData. How can I achieve this date format?
This looks like a job for: DateTime:createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-02 11:44:24');
echo $date->format('Y-m-d\TH:i:s');
Once you've got the DateTime object then you can format it as needed with ->format()
The date and time can be tricky, but what you want to be sure of is the date and time for your region or Greenwich Mean Time (GMT) or Universal Time Coordinate (UTC). Both GMT and UTC refer to the same time, but your server/database/publish date is likely in your local time. Here's an example of what you can use for local time:
date_default_timezone_set("America/New_York");
$date = date('Y-m-d\TG:i:s');
echo $date;
This will echo 2017-11-02T01:02:58. The capital G represents a 24 hour time stamp, and a small g will represent a 12 hour time stamp.
Depending on how your server is setup, without the date_default_timezone_set you will get the UTC time.
So I am having a little trouble getting today's midnight date using the midnight time using the time() function.
It is a little confusing for me when I explain to my peers on the timestamp I want. What I need is, if today is wed-06-Aug-2014 at 9:00pm , then I want the time at wed-06-Aug-2014 00:00.
Any help?
Just use relative date and time formats:
echo date('D-d-M-Y H:i', strtotime('midnight'));
Demo
I'm having a table as follows to store future date,
email date
abc#gmail.com 8/10/2014
and I want to do is to find the difference between the above date and the sever date.
I'm using date("m/d/Y") to get the current date.
If date("m/d/Y") = 07/20/2014, then I need the answer as 21.
Please help me find the difference between those days using PHP & MySQL, or suggest a better way to find the difference in days.
You can convert date to timestamp then calculate the days:
(int)(strtotime('8/10/2014')-strtotime('07/20/2014'))/60/60/24
The easiest way would be to store your dates as timestamps, then you could subtract the current timestamp with the one you've saved in a database.
The PHP function time() returns the current timestamp – that is the number of seconds since the 1st of January 1970. You can then format a timestamp $stamp to your liking with date('m/d/Y', $stamp), for example.
Aside from facilitating arithmetic operations with dates, you can display more or less information with timestamps by formatting them with date(), as well as show different formats (July 13, 2014 vs. 07/13/2014). If you save a date as a string, e.g. "8/10/2014", it will be very complicated for you to change the format, and it won't be possible to get the correct time, for example.
Finding how long ago in days a timestamp $stamp was to the current time is very easy:
$now = time();
$days = ($stamp - $now) / (24*3600);
Use round() to get a full number if desired (e.g. 7.2309 would simply become 7).
I'd personally do it using DateTime:
Select your date out into a variable. In this instance We'll call it $DBDate
$DBDateObj = DateTime::createFromFormat('j/m/Y', $DBDate);
$TodayDateObj = new DateTime();
$interval = $TodayDateObj->diff($DBDateObj);
$daysDiff = $interval->days;
In $daysDiff you should now have the difference in days.
I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
I need to make a timestamp to put into MySQL. The user is submitting a number (of weeks) I need to add that many weeks to today's date. What I am trying to do is calculate an end date of an ad that the user is submitting.
Any suggestions on how to do this? Thanks!
You can use strtotime to add time to a date - it takes a second argument that is the current time if none is passed. You can then pass that created time to the date function to create your timestamp:
$timestamp = date('Y-m-d H:i:s', strtotime('+10 weeks'));
I think DATE_ADD(CURDATE(), INTERVAL 2 WEEK) would add 2 weekss to the current date, for instance
If you want the ad to expire at a certain time of day you can use mktime:
$day = date("d") + ($weeks * 7);
mktime($hour,$minute,$second,$month,$day,$year);
Just to add, some would say that storing the UNIX time as a plain-old int field in the database is the more flexible and portable solution. Furthermore, inserts and updates happen faster because they only involve storing simple integers. It really depends on how much date manipulation you need to do at the database level. I tend to go for portability and do all my date calculations in PHP. To store the current timestamp, I would just insert into an integer column the output of:
strtotime('now');
or
time();
which both return the current (unix) timestamp. Date comparison thereafter can be done by fetching timestamps from the database and performing simple arithmetical operation, as trivial as:
if($tsFromDb > strtotime('+28 days')) {
echo 'it is the future, zombies!';
}
It really depends on what you're using dates for.