I'm struggling with the PHP-DateTime Object.
I just want to set the beginn date to the start of the current year and the end date to the end of the current year.
Is there an easy way to create such a date in one line?
$beg = new DateTime();//TO-DO 01.01.ThisYear
$this->beginDate = $beg;
$end = new DateTime();//TO-DO EndOfThisYear
$this->endDate = $end;
I'm using an older PHP-Version, don't know exactly which one...something above 5.
Sure:
$beg = new DateTime(date('Y-01-01'));
$end = new DateTime(date('Y-12-31'));
Related
I need to find 15 minute intervals between two times without date it all works fine if both times are before 00:00. I am passing times like: 17:00, 23:00 and 01:30.
After midnight is the problem of course it is another day so this is the reason it fails. I wanted know if there was a way to find the intervals regardless because the end time could be any eg. before or after midnight so adding in a real date may not be ideal, it will only be few hours after midnight nothing more than that.
$period = new DatePeriod(
new DateTime("17:00"),
new DateInterval('PT15M'),
new DateTime("01:00")
);
Fiddle
Returns nothing, any help would be much appreciated.
If the end-time is shorter than the start-time, I don't see any option other than adding 1 day to the end-time.
$start = "17:00";
$end = "01:30";
$dtStart = date_create('today '.$start);
$dtEnd = date_create('today '.$end);
if($dtEnd < $dtStart){
$dtEnd->modify('+1 day');
}
$period = new DatePeriod(
$dtStart,
new DateInterval('PT15M'),
$dtEnd
);
Because you're using only the time portion, the day will be automatically set to the current day.. which makes the end being smaller than the begin.
But if you take real dates (which ones doesn't really matter) and pick the next day at 1'clock...
$period = new DatePeriod(
new DateTime("1970-01-01 17:00"),
new DateInterval('PT15M'),
new DateTime("1970-01-02 01:00")
);
foreach ($period as $date) {
echo $date->format("H:i").' - ';
}
hth
I am trying to create an application in PHP that have a subscription period of 14 days. In my db table, I have start date and expiry date. Iam a bit confused to show up the expiration banner that says "Your application will expire in --- days" .
$start = explode(' ', $billing->started_at); // to get the date only
$end = explode(' ', $billing->expires_at);
$date1=date_create($start[0]);
$date2=date_create($end[0]);
$diff=date_diff($date1,$date2);
Session::put('days_left', $diff->format("%R%a days"));
Please help
In theory, your code should work but you didn't tell us what the actual issue is so here's how I'd approach it.
If you only want to calculate using the date portion of your timestamps, you can just format it before creating your DateTime objects.
<?php
$start = new DateTime(date('Y-m-d', strtotime($billing->started_at)));
$end = new DateTime(date('Y-m-d', strtotime($billing->expires_at)));
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Alternatively, you can simply use the timestamp directly. Depends on what you are trying to achieve.
<?php
$start = new DateTime($billing->started_at);
$end = new DateTime($billing->expires_at);
$difference = $start->diff($end);
echo $difference->format("Your application will expire in %a days.");
Working example:
http://sandbox.onlinephpfunctions.com/code/417d4691483f53fe083735d257df4fb49b832c58
How do I remove time from a date in PHP, for example:
20170803173418 I want to take 4 minutes and 13 seconds away and get the new datestamp that would be 20170803173005
What code do I use to get this?
EDIT
I currently have:
$dbTime = $row['aptDeadline']; // This is the appointment end time stored in DB
$dbTime = new DateTime($dbTime);
$currentTime = date("YmdHis"); // This is the current time
$currentTime = new DateTime($currentTime);
$counterTime = $row['aptDeadline']; //This is the time a countdown clock works from inDB
$counterTime = new DateTime($counterTime);
$difference = $currentTime->diff(new DateTime($dbTime)); // Calculate the time between now and the apt time in the db
I now need some code that if the $difference is positive, can take this figure away from the $counterTime stamp
You can use the modify method of the DateTime class in PHP:
<?php
$time = new \DateTime('20170803173418');
$time->modify('-4 minutes')->modify('-13 seconds');
echo $time->format('YmdHis');
This will print the result you want.
I want to change date var when x days have passed
For instance:
Today is 21.12.16 - $date = '23.12.16'
Tomorrow is 22.12.16 - $date = '23.12.16'
When it's 23.12.16 - $date = '25.12.16'
Her's the code I got so far. Hope this will make some sense
$date = "2016-12-21"; //** will describe this lower
$days_passed = date_create()->diff(date_create($date))->days;
if ($days_passed >= 2){
$new_date = date('d.m.y', strtotime("+2 days"));
} else{
$new_date = $date;
}
This works ok if I just want to do it once
**I need to change this var every 2 days. I understand that i can write it to a Database or to a .txt. But there sure is a way to do this just by php
P.S. sorry for my bad English.
Here's what I came up with:
$date = '2016-12-01'; //Your script start date, you wont need to change this anymore
$everyxdate = 10; // once x days to add x to $date
$days_passed = date_create()->diff(date_create($date))->days; // passed days from start of script $date
$mod_dates = (int)($days_passed / $everyxdate); // count how much cycles have passed
$daystoadd = $mod_dates * $everyxdate + $everyxdate; // count how much days we need to add
$newdate = strtotime ("+$daystoadd day" , strtotime ( $date ) ) ; // Add needed day count to starting $date
$newdate = date ( 'd.m.y' , $newdate ); // Format date the way you want
Hope this will help some one who has the same task I had.
On my website, I have entries in the format TITLE#HH:MM-HH:MM#LOCATION. I can easily split this into 3 different array keys with explode("#",..., and display the data, but now I am working on creating a function to return whether the current timestamp is within the "HH:MM-HH:MM".
HH:MM-HH:MM is in the format of 12:20-13:10, where the first time is the start time of the event, and the second time is when the event ends. I'm trying to detect whether the time the page loads and time() is called is within 12:20-13:10.
How would I go about doing this? I have tried creating some code but I'm not sure how to accomplish this cleanly.
Thanks!
UPDATE --
$tEvent = explode("-", $arr[1]);
$now = new DateTime(date("Y-m-d H:i:s",$time), new DateTimeZone('America/Detroit'));
$start = new DateTime($tEvent[0], new DateTimeZone('America/Detroit'));
$end = new DateTime($tEvent[1], new DateTimeZone('America/Detroit'));
if ($now->format('U') >= $start->format('U') && $now->format('U') <= $end->format('U')) {
//within time
}
You can do this with several different methods. Personally, I like to work with Datetime class to manipulate dates in php.
$timeEvent = "12:20-13:10"; // your string with start and end of the event
$tEvent = explode("-", $timeEvent); // split in 2 (0 is start, 1 is end)
$now = new Datetime("NOW"); // gets the actual date
$start = new Datetime($tEvent[0]); // create datetime object with start date
$end = new Datetime($tEvent[1]); // create datetime object with end date
if ( $now > $start && $now < $end ) // check if now is between start and end
{
echo "It's event time!";
}