yii2 pick time from datetime - php

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');

Related

php adding 1 hour to the timestamp

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

How to extend date in PHP?

I have developed a subscription website. I am facing php date extend problem.
Please read my message-
Suppose today i have subscribe a package. Package duration 1 month. I mean 2014-05-08 to 2014-06-07
After two days i will again subscribe this same package. now the Package duration will extend 2014-06-07 to 2014-07-07
In my PHP code i am getting current date to +1 month but how to get after 1 month to next one month?
I have used this code:
date('Y-m-d h:i:s', mktime(0, 0, 0, date("m")+$getSubscription['Subscription']['duration'], date("d"), date("Y")));
You can use strtotime to achieve this.
Check this out:
<?php
// current subscription expiry date
$day = '2014-06-07';
// add 30 days to the date above
$new_date = date('Y-m-d', strtotime($day . " +30 days"));
// debug
echo $new_date;
Outputs: 2014-07-07
P.s. are you saving the newly calculated date into db or something and then using it on the next calculation?
Or use the DateTime object :
<?php
$date = new DateTime('2014-06-07');
$date_end = clone $date;
$date_end->add(new DateInterval('P1M');
echo $date_end->format('Y-m-d');
You'd save yourself a lot of hassle by using DateTime objects instead of messing around with date() and mktime(). In order to get today, simply:
$subscriptionDate = new DateTime(); // 2014-05-08
Then you can use the DateInterval class to add additional months, so if it's a monthly subscription, simply:
$subscriptionPeriod = new DateInterval('P1M');
$subscriptionDate->add($subscriptionPeriod);
Now, $subscriptionDate will be at 2014-06-08. You can keep adding as many months as you want and save it to your database, in the format you want, like:
$subscriptionDate->format('Y-m-d'); // 2014-06-08
$subscriptionDate->format('Y-m-d H:i:s'); // incl. time, e.g. 2014-06-08 12:02:45
See the manual for more examples.

how to decrease 5 month from the today date in codeigniter php

i have today date like 2013-03-09 02:12:36 i want to get the output like 2012-10-09 02:12:36 (5 month ago time)
I am getting today with code
$datestring = "%Y-%m-%d: %d:%h:%i";
$time = time();
$today=mdate($datestring, $time);//gives me2013-03-09 02:12:36``
What i want is
$sixmonth= $today - 6 month//how to get this output
Please help me.
You can use a function named strtotime(), this will do what you want. You should read up about how the function works here. You should also look into the date() function to format your dates and times, you can do that so here.
echo date("Y-m-d H:i:s", strtotime("-6 months"));
Example output:
root#upstairslinux:~# php 6months.php
2012-09-09 12:20:23
root#upstairslinux:~#

PHP Calculating future date by adding days to a variable date

I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));

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