I have several date ranges in form of DateTime $begin, DateTime $end. Those ranges can overlap in every possible way: etc. What I am trying to do is to get length (in seconds or DateInterval) of those ranges between start of the first one and the end of the latest one (fourth in the case above), excluding regions not covered
Tag: datetime
Find difference between two datetimes and format at Y-m-d H:i:s
I’m trying to get the difference between two datetimes and return it as a datetime. I’ve found examples using diff but I can’t seem to get it right. However $totaltime logs 0000-00-00 00:00:00 to my DB. Is this because I’m not formatting my totaltime variable? Answer I’m not sure what format you’re looking for in your difference but here’s how
Date and time in Greek
I’m currently using a website to get the time in Athens: $d = new DateTime(“now”, new DateTimeZone(“Europe/Athens”)); echo $d->format(“l, d M Y”); But I would like the date to be displayed in …
How to show time with php code with the right timezone
I need to show current time on my site in my city Iran/Tehran I’m using this code: but it doesn’t show the right time. my country timezone : iran/tehran my city …
Add six months in php
I’m trying to get the month, six months out from the current date. I’ve tried using: date(‘d’, strtotime(‘+6 month’, time())); But it doesn’t seem to work, always returns 01. Is there a better way to do this? Thank you! Answer I find working with DateTime much easier to use: or or in PHP version 5.4+
Add ‘x’ number of hours to date
I currently have php returning the current date/time like so: What I’d like to do is have a new variable $new_time equal $now + $hours, where $hours is a number of hours ranging from 24 to 800. Any suggestions? Answer You may use something like the strtotime() function to add something to the current timestamp. $new_time = date(“Y-m-d H:i:s”, strtotime(‘+5
Convert PHP date into javascript date format
I have a PHP script that outputs an array of data. This is then transformed into JSON using the json_encode() function. My issue is I have a date within my array and it’s not in the correct JavaScript format. How can I convert this within PHP so it is? Instead of the above for the date I need the equivalent
Getting start and end date of a PHP DatePeriod object?
How can i get the start and end date of a DatePeriod object? Answer DatePeriod only implements the Traversable interface and has no other methods to either access elements or retrieve them. You can do something easy to get start/end dates:
Adding 1 hour to time variable
I have a time to which I want to add an hour: I’ve tried: But none of the above work. Answer Worked for me.. Explanation: strtotime(’10:09′) creates a numerical timestamp in seconds, something like 1510450372. Simply add or remove the amount of seconds you need and use date() to convert it back into a human readable format. time() also creates
Getting first / last date of the week
Is it possible to get the first / last date of a week using PHP’s Relative Date Time format? I’ve tried to do: As you can see it doesn’t output the correct dates. According to the docs this should be possible if I’m correct. Am I doing something terrible wrong? EDIT I need to use PHP’s DateTime for dates in