how to decrease 5 month from the today date in codeigniter php - 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:~#

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

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

PHP: Two same date statements but day of the week is wrong in one of them

I've written this code:
if(!is_null($customDate)){
$referenceDate = strtotime($customDate);
$toDay = date("N", $referenceDate);
echo("/".$referenceDate."/".$toDay."/");
}
else{
$referenceDate = strtotime(date("d:m:y"));
$toDay = date("N");
echo("/".$referenceDate."/".$toDay."/");
}
if I supply $customDate like:
function_name("24:07:17");
it prints out this:
/1500941237/2/
A datestamp (I assume) and 2 as day of week - Tuesday.
If I don't supply $customDate and call function as:
function_name();
I get this:
/1500941237/1/
Which is again - datestamp (I assume) and 1 for day of Week - Monday.
The second one is correct, it's 24th and it's Monday.
I am pretty new to PHP so I am almost 100% sure there is some small nuance in regards to operations with dates I am doing but I am not sure what it is.
How can two identical datestamps produce difference day of week?
Running it on WAMP server on Windows 10 with default config.
Full function:
function nextDate($customDate=null){
$referenceDate;
$toDay;
if(!is_null($customDate)){
$referenceDate = strtotime($customDate);
$toDay = date("N", $referenceDate);
echo("/".$referenceDate."/".$toDay."/");
}
else{
$referenceDate = strtotime(date("d:m:y"));
$toDay = date("N");
echo("/".$referenceDate."/".$toDay."/");
}
}
nextDate("24:07:17"); --> gives wrong result, it says 24th is Tuesday.
nextDate(); --> gives correct result, it says 24th is Monday.
it's because 24:07:17 is not valid date format, PHP thinks it's a time so "today 24:07:17"
echo date("Y-m-d H:i:s", strtotime("24:07:17"));
// outputs 2017-07-25 00:07:17
use valid date format or convert it to something strtotime will understand correctly:
list($d,$m,$y) = explode(":", "24:07:17");
$referenceDate = strtotime("20{$y}-{$m}-{$d}");

Adjust a PHP date to the current year

I have a PHP date in a database, for example 8th August 2011. I have this date in a strtotime() format so I can display it as I please.
I need to adjust this date to make it 8th August 2013 (current year). What is the best way of doing this? So far, I've been racking my brains but to no avail.
Some of the answers you have so far have missed the point that you want to update any given date to the current year and have concentrated on turning 2011 into 2013, excluding the accepted answer. However, I feel that examples using the DateTime classes are always of use in these cases.
The accepted answer will result in a Notice:-
Notice: A non well formed numeric value encountered......
if your supplied date is the 29th February on a Leapyear, although it should still give the correct result.
Here is a generic function that will take any valid date and return the same date in the current year:-
/**
* #param String $dateString
* #return DateTime
*/
function updateDate($dateString){
$suppliedDate = new \DateTime($dateString);
$currentYear = (int)(new \DateTime())->format('Y');
return (new \DateTime())->setDate($currentYear, (int)$suppliedDate->format('m'), (int)$suppliedDate->format('d'));
}
For example:-
var_dump(updateDate('8th August 2011'));
See it working here and see the PHP manual for more information on the DateTime classes.
You don't say how you want to use the updated date, but DateTime is flexible enough to allow you to do with it as you wish. I would draw your attention to the DateTime::format() method as being particularly useful.
strtotime( date( 'd M ', $originaleDate ) . date( 'Y' ) );
This takes the day and month of the original time, adds the current year, and converts it to the new date.
You can also add the amount of seconds you want to add to the original timestamp. For 2 years this would be 63 113 852 seconds.
You could retrieve the timestamp of the same date two years later with strtotime() first parameter and then convert it in the format you want to display.
<?php
$date = "11/08/2011";
$time = strtotime($date);
$time_future = strtotime("+2 years", $time);
$future = date("d/m/Y", $time_future);
echo "NEW DATE : " . $future;
?>
You can for instance output it like this:
date('2013-m-d', strtotime($myTime))
Just like that... or use
$year = date('Y');
$myMonthDay = date('m-d', strtotime($myTime));
echo $year . '-' . $myMonthDay;
Use the date modify function Like this
$date = new DateTime('2011-08-08');
$date->modify('+2 years');
echo $date->format('Y-m-d') . "\n";
//will give "2013-08-08"

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

Categories