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
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.
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 need to generate a timestamp for an exact date in the next month.
I want to generate the timestamp for the 10th of the next month automatically.
I tried playing around with strtotime but couldn't work it out.
Thanks,
Try this:
echo date('Y-m-d', strtotime(date('Y-m-10'). '+1 month'));
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)
...
Assume I have a funny video site that shows funny videos to users. Below every video, I want to have a statement that says "5 seconds ago", "31 minutes ago", "Yesterday", "2 days ago" based on a timestamp of the video and the current time.
The timestamp that I have is in the format: 2011-10-17 07:08:00.
I'm not too good with dates, but I'm guessing that I need to find the difference between the 2 date/time in seconds, then decide if its between 0sec & 60sec to display in seconds, or between 60sec & 3600sec to display in minutes, or between 3600sec & 3600x24sec to display in hours, between 3600x24sec & 3600x24x2sec to display yesterday, and so on... I believe I should be using strtotime() but I cant seem to find the current time as those solutions I found used new date() which does not seem to work!
How can I do this?
Btw, side question, when I insert 2011-10-17, 7:08PM EDT into a MySQL timestamp column, it gets converted to 2011-10-17 07:08:00 which is AM. How can I get it to be stored in the correct AM/PM?
You can use the DateTime functions of php.
$datetime1 = new DateTime('2011-10-17 07:08:00');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
From here on you can use some if-statements to output the time difference in another format (seconds, minutes, hours, month, etc.) depending on the actual time difference! The formats for the output are to find here
You can very easily use the DATEDIFF and TIMEDIFF functions of MySQL. Both together tell you exactly how much time has passed.