strtotime with variable using php - object given - php

This is my current code:
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year");
$arrivalTime = date('Y-M-d H:i:s', strtotime("+$flightTimeMin minutes", $dateGame));
This isn't working because I believe "$dateGame" is an object. How would I turn it into something readable by "strtotime"?
Thanks

You dont need to use strtotime() since you are using datetime and you can use datetime object to format the date as
$universeTime = 3 ;
$flightTimeMin = 20;
$dateGame = new DateTime();
date_modify($dateGame, "+$universeTime Year +$flightTimeMin minutes");
If you want to format the display you can use as
echo $dateGame->format('Y-m-d H:i:s');
or just use
echo $dateGame->date ;

Related

PHP: why year not subtracted from date?

The below code doesn't subtract 1 year from the date. Why?
$date1 = '2021-06-02';
$date2 = new \DateTime(date($date1, strtotime('-1 year')));
echo $date2->format('Y-m-d'); // outputs the same date 2021-06-02
Part of your problem is that the date function's first argument is the format of the date.
https://www.php.net/manual/en/function.date.php
So what is happening is that you are creating a date string with the format of '2021-06-02'.
https://www.php.net/manual/en/datetime.format.php
This doesn't use anything from the timestamp that you are providing so this string is passed to the constructor of DateTime and creating the date instead of the one from the year previous.
Please use this code. Its always works for me.
$date1 = '2021-06-02';
$date2 = date("Y-m-d", strtotime("-1 year", strtotime($date1)));
echo $date2; //Output 2020-06-02
This is for Date time object:
$dt = new DateTime('2021-06-02');
$minusOneYearDT = $dt->sub(new DateInterval('P1Y'));
$minusOneYear = $minusOneYearDT->format('Y-m-d');
echo $minusOneYear;
OR make a small solution:
$time = new DateTime('2021-06-02');
$newtime = $time->modify('-1 year')->format('Y-m-d');
echo $newtime;
Your code is a bit of a muddle:
date() takes as parameters a format string, and an integer representing a point in time; it then applies the format to create a string for that date and time
strtotime() takes a string, interprets it as a point in time, and returns an integer timestamp
new DateTime() takes a string, in any of the formats strtotime would accept, but creates an object representation rather than returning an integer
You've tried to use all of them at once, and got in a mess:
Your call to date() has a first parameter of '2021-06-02', which isn't a valid format.
Your call to strtotime() has a parameter of '-1 year', which will just be interpreted as "1 year before now", not relative to anything else you've specified.
Using both of those functions and then passing to new \DateTime() doesn't make a lot of sense, since the object can do all the same things those functions can do.
If you want to use the integer-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = strtotime("$date1 -1 year");
echo date('Y-m-d', $date2);
If you want to use the object-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = new \DateTime("$date1 -1 year");
echo $date2->format('Y-m-d');
Or this (note the use of DateTimeImmutable instead of DateTime to avoid the modify method changing the $date1 object:
$date1 = new \DateTimeImmutable('2021-06-02');
$date2 = $date1->modify('-1 year');
echo $date2->format('Y-m-d');

PHP date adding in DateTime object does not work

Try to do:
$datetime = new DateTime();
$onehour = new DateInterval('PT1H');
$datetime->add($onehour);
$timestamp = $datetime->date;
echo $timestamp; // nothing
What's wrong with it?
There's no such date property in the DateTime object.
Instead you can use format() to get the date formatted according to given format.
echo $datetime->format('Y-m-d H:i:s');
or use getTimestamp() to get the Unix timestamp:
echo $datetime->getTimestamp();
http://php.net/manual/en/datetime.format.php
http://php.net/manual/en/datetime.gettimestamp.php
You have to use :
DateInterval::createFromDateString("1 hours");
Instead of :
new DateInterval('PT1H');
As Given Below Code :
$onehour = DateInterval::createFromDateString("1 hours");
$date = new DateTime();
$date->add($onehour);
echo $date->format("Y-m-d H:i:s");

Setting Php and Mysql timestamp

I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately
In my database, I have their columns as timestamp.
I know I could do something similar to this but am not sure if it's correct.
// For 4 hours ahead of time
$dt2 = date("Y-m-d 04:i:s");
//For 2 days ahead
$dt2 = date("Y-m-02 H:i:s");
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));
//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
In my mind it's much better to work with DateTime field and the DateTime class.
You have the ability so modify that objects very easily. For example:
$aktDate = new \DateTime();
Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.
$aktDate = new \DateTime('Y-m-d 04:i:s');
Not you can modify your dates if you want with the modify function.
in your case:
$pastDate = clone $aktDate;
$pastDate->modify('+2 days');
$futureDate = clone $aktDate;
$futureDate->modify('+4 days');
if($pastDate < $aktDate && $aktDate < $futureDate) {
// do something
}
I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');
$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');
You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).
For your examples:
//+2 hours<br>
strtotime("+2 hours");
// +2 days<br>
strtotime("+2 days")
Edit: for what you ask, about posted values, the syntax is like this:
strtotime("+2".$_POST['field_name']." days");
You can use hours/days/months/weeks/years and either + or -

Add minutes to datetime string

$datetime_from = '2013-08-27 14:17:00';
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));
The result is:
$datetime_till = '1970-01-01 01:00:00'
The expected result is
$datetime_till = '2013-08-27 15:02'
How to get it?
It will be like
$datetime_from = strtotime('2013-08-27 14:17:00');
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));
You need to convert $datetime_from to time
Orelse you can also try like(Iam not sure)
$dateTime = DateTime::createFromFormat('m/d/Y h:i', $datetime_from);
$datetime_from = $dateTime->format('U');
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));
Try-
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",strtotime($datetime_from)));
You should use the DateTime class instead of old functional php.
$dateFrom = new \DateTime('2013-08-27 14:17:00');
$dateTill = new \DateTime('2013-08-27 14:17:00');
$dateTill->modify('+45 minutes');
// test if the dates are correct
echo $dateFrom->format('Y-m-d H:i:s'). ' - '.$dateTill->format('Y-m-d H:i:s');
There are numerous other methods you can benefit from the DateTime class.
You can use the DateTime object to do this in procedural style PHP like this:
$datetime_from = date_create('2013-08-27 14:17:00'); // Create a date object from the start date
$datetime_till = date_add($datetime_from, date_interval_create_from_date_string('45 minutes')); // Add the interval to the starting time
echo date_format($datetime_till, 'Y-m-d H:i'); // Format the date how you want it to be output
Hope this helps.
Use:
$datetime_from = strtotime('2013-08-27 14:17:00');
$datetime_till = date("Y-m-d H:i",$datetime_from+(45*60));
Use DateTime class for date/time modifications :
$datetime_from = new DateTime('2013-08-27 14:17:00');
$datetime_till = clone $datetime_from;
$datetime_till->modify('+45 minutes');
echo
'From: ' . $datetime_from->format('Y-m-d H:i:s') . "\n".
'Till: ' . $datetime_till->format('Y-m-d H:i:s');
Output will be :
From: 2013-08-27 14:17:00
Till: 2013-08-27 15:02:00
Valid modify() formats are explained in Date and Time Formats.
Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00 and 2013-03-30 21:00. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800 seconds which is 0.95833333333333 days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Categories