php adding 1 hour to the timestamp - php

I am trying to add 1 hour to a timestamp field fetched from database using the following code.
date($ls['created_at'], strtotime('+1 hour'));
However, this doesn't seem to work. It returns the same time as in database. Am I missing something? Or, is the code deprecated? What is the proper solution?

You need to give it the correct syntax to use this,
You need to send the time to change with the change itself in the function - for example (using date for wanted format):
$date = "22-02-2021 14:22:22";
echo date("d-m-Y H:i:s", strtotime($date.' +1 hour'));
This will return:
22-02-2021 15:22:22
Same as this:
echo date("d-m-Y H:i:s", strtotime("22-02-2021 14:22:22 + 1 hour"));
The idea is that you strtotime receives the date and data to change in one string like this :
echo strtotime("22-02-2021 14:22:22 + 2 hour");
Will return:
1614010942
Here I removed the Date Format so I received a unix timestamp format

Related

How to convert iso date time with milliseconds to date time format in php so that milliseconds don't go away?

For eg I have ISO date time as : 2020-03-03T11:07:41.1708478Z
Converting using strtotime function
$dateTime = date("Y-m-d H:i:s.u",strtotime('2020-03-03T11:07:41.1708478Z'));
Result : 2020-03-03 11:07:41.000000
In above result you can see milliseconds are gone.
Use DateTime because strtotime and date only uses full seconds.
$date = new DateTime('2020-03-03T11:07:41.1708478Z');
echo $date->format("Y-m-d H:i:s.u"); // 2020-03-03 11:07:41.170847
https://3v4l.org/DXVj8
But since this I assume this input format is rater fixed and wont change I could recommend you to use a simple str_replace.
echo str_replace(["T","Z"], [" ",""], '2020-03-03T11:07:41.1708478Z'); // 2020-03-03 11:07:41.170847

yii2 pick time from datetime

I want to know how to pick the time format from datetime in yii2.
So if there is code like this
$model->date = date('Y-m-d H:i:s', strtotime('+5 hours'));
and the result would be 2018-05-08 23:36:21
how can I extract the time only? so the result is only 23:36:21
I already tried using code below
date("H:i:s", strtotime('-30 minutes'));
but I only got like 00:30:00
Is there something wrong with the code?
Any help would be appreciated :)
if you have the string 2018-05-08 23:36:21 and want to extract time, you can follow the following methods
Using php:date() function
Remove -30m from the time when you format the date
echo date('H:i:s',strtotime('2018-05-08 23:36:21 -30 minutes'));
Using DateTime Object
The method sub() can subtract from the time and output the remaining time using $dateTimeObj->format().
$date1=new \DateTime('2018-05-08 23:36:21');
$date1->sub(new \DateInterval('PT30M'));
echo $date1->format('H:i:s');

How to update certain values of a php date string?

I have some date string values that I want to be able to update for checking against in some conditional statements. I want to update the hour, minute and seconds values to be at 23:59:59.
Say I have the variable $value which prints to
2017-03-08 00:00:00
How can I update the value to be
2017-03-08 23:59:59
?
Use DateTime.
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s',$value);
$dateTime->setTime(23,59,59);
$value = $dateTime->format('Y-m-d H:i:s');
http://php.net/manual/en/class.datetime.php
You could do many more things with the DateTime class.
Hi there this is the code below which you can use to update the time value:
your variable in php:
$value = "2017-03-08 00:00:00";
the html tag like so:
<input type="datetime-local" name="date" value="<?php echo date("Y-m-d\TH:i:s", strtotime($value)); ?>" />
This tag also generates a date and time picker, assuming you a running a compatible browser.
Note: In the future please show us your work and what you have achieved because you cannot expect someone to do it all for you, good luck!
In this simple case, you could probably leverage strtotime to get what you want:
$myDate = '2017-03-08 00:00:00';
$myTime = strtotime($myDate) + 86399;
echo date('Y-m-d H:i:s', $myTime);
Though in more difficult cases it would probably be better to use PHP's DateTime class:
$myDate = '2017-03-08 00:00:00';
$dt = new DateTime($myDate);
// Subtract one second
$dt->add(new DateInterval('PT86399S'));
// Output formatted result
echo $dt->format('Y-m-d H:i:s');
You could also look into Carbon for handling all of your date / time needs.

DateTime defaulting to 1970-01-01

I am writing a PHP form for my website. The HTML side asks the user for a date which they enter in MM/DD/YYYY format. When that string is sent to PHP the following code changes it to the form that MySQL will recognize
$date = $_POST['date'];
$sqldate = date('Y-m-d', strtotime($date));
However when that date is entered into my MySQL database it is entered as 1970-01-01 and I can't figure out why.
NOTE: If I echo $sqldate I get the error Use of undefined constant sqldate - assumed 'sqldate' in C:/MYDIRECTORY
How about this?
//build a date
$date = date_parse_from_format("m/d/Y", $_POST["date"]);
//output the bits
$sqldate = "$date[year]-$date[month]-$date[day]";
A Unix timestamp is the number of seconds since 1970-01-01 so your call to strtotime() is returning 0.
How is DateTime working for you?
<?php
$dateStr = '08/18/2014';//$_POST['date'];
$dateTime = new DateTime($dateStr);
echo $dateTime->format('Y-m-d');
You're writing that the date format of the form is MM/DD/YYYY. Do you validate the input values to be sure that the format is always given?
THis worked for me
$input_date= trim($_POST["input_date"]);
$strtotime= strtotime($input_date);
$date_format= date('Y-m-d',$strtotime);

php date time function

I want to convert this date ( 02-12-2010) mm-dd-yyyy to time format
ie
to 02-12-2010 0 hours 0 minutes and 0 seconds
i have user time and date functions but when i refresh the page its value is changing as per the time.
i need that to be fixed
also i want to convert this date (01-24-2009) to time format.
please help me
Thanks
Use the strtotime function to convert an existing date/time string into a timestamp for the date function.
$new_date = date('m-d-Y h:i:s', strtotime('02-12-2010'));
The reason that your date keeps updating with the current time is that the date() function uses the current system's timestamp by default when no second parameter is provided.
Use the date_parse_from_format () API function to transform your given format to an array. E.g.
$date = "02-12-2010";
$dateArr = date_parse_from_format("d-m-Y", $date);
Output it with something like:
$output = $dateArr['day'] . '-' . $dateArr['month'] '-' . $dateArr['year'];
You can use the strtotime function eg:
$mydate = date('m-d-Y h:i:s', strtotime('02-12-2010'));

Categories