How to change date format to strtotime in codeigniter - php

I want to convert date format to strtotime in codeigniter to cross check the expire date from the database.
My code look like this below
$expiredate = date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s",strtotime($tra->t_started)). '+30days'));
if(strtotime(date("Y-m-d H:i:s")) > strtotime($expiredate)){
$ra->is_paid="1";
//package is expired for 30days.
} else{
$ra->is_paid="0";
//package is expired for 30days.
}
I need help to solve this problems. Thanks

Several issues here:
First, you don't need to convert dates with strtotime to compare them.
Second, to add a certain amount of days to a date, you use +30 day (singular) not +30 days
You could simply:
$now = date('Y-m-d H:i:s');
$expire_date = date('Y-m-d H:i:s', strtotime($tra->t_started."+30 day"));
if ($now >= $expire_date)
{
// 30 or more days overdue
}
else
{
// Less than 30 days overdue
}

Related

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

timestamp check if 10 minutes ago

I have a data in a text file saved using date("Y-m-d h:i:s", strtotime("+2 minutes")) that I need to check if it's 10 minutes ago. I'm trying the following code but it doesn't print anything even if more than 10 minutes ago.
$now = date("Y-m-d h:i:s", strtotime("now"));
if($now > strtotime($old_data))
echo 'expired!';
your comparing a formatted date with a time stamp which explains why nothing works
here:
$now = strtotime("-10 minutes");
if ($now > strtotime($old_data) {
echo 'expired!';
}
You should change either of the following:
$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));
or
if (strtotime($now) > strtotime($old_data))
I'll go for the second. You are comparing a timestamp over date that is why you are not satisfying the condition of if().
Furthermore, you can also use time() if you're only concern is the current timestamp.
$now = time();
or
$now = date("Y-m-d h:i:s", strtotime("now")); // Remove this line
if(time() > strtotime($old_data)) // Change $now with time()

how to check if a date is three days before today

Hey i would like to know if there is any script (php) that could check if a specified date three days before today.
say..
$d1 = date("Y-m-d", filemtime($testfile));
$d2 = date("Y-m-d");
now i would like to know how to compare this two dates to check if d1 is atleast 3days ago or before d2
any help would be gladly appreciated.
Why not to use DateTime object.
$d1 = new DateTime(date('Y-m-d',filemtime($testfile));
$d2 = new DateTime(date('Y-m-d'));
$interval = $d1->diff($d2);
$diff = $interval->format('%a');
if($diff>3){
}
else {
}
Assuming you wish to test whether the file was modified more than three days ago:
if (filemtime($testfile) < strtotime('-3 days')) {
// file modification time is more than three days ago
}
Just check it with timestamp:
if (time() - filemtime($testfile) >= 3 * 86400) {
// ...
}
use date("Y-m-d", strtotime("-3 day")); for specific date
you can also use
strtotime(date("Y-m-d", strtotime("-3 day")));
to convert it to integer before comparing a date string
well, stunned to see no one is using mktime() function,
it makes the job simple
for example your input date is :10/10/2012
mktime convert it to unix time stamp
$check_date=mktime(0,0,0,10,**10+3**,2012);
we can perform any operations weather +,-,*,/
use timestamp instead of date,
$d1 = filemtime($testfile);
$now = time();
if ($now - $d1 > 3600*24*3) {
..
}

Create Unix Time Stamp of next day at 10am

Hi Suppose I have a timestamp of "2005-10-16 13:05:41".
How would I go about creating a variable that will have a unixtime of the next time it becomes 10am from that initial point?
Would it be something like this?
$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);
I am guessing there is some string I can use to do this? Like "next thursday" example in the PHP documentation.
You nearly had it...
$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);
Edit:
This was based on the title of your question, for the timestamp of 10am the next day. If you want to output 10am the same day for any times before 10am then you'll want to add some extra logic, as thatidiotguy suggested.
Edit2:
For some reason it won't work if you put all the logic in the same strtotime method, so I made a simple function. You could easily put this into a single line, but I left it as 2 to make it clearer:
$time1 = strtotime('-2 days 09:59:59');
$time2 = strtotime('-2 days 10:00:01');
function next_10am($time)
{
$temp = strtotime('+1 day -10 hours', $time);
return strtotime('10:00', $temp);
}
echo next_10am($time1); // Outputs: 2012-09-08 10:00:00
echo next_10am($time2); // Outputs: 2012-09-09 10:00:00
There is no way for strtotime to know whether or not 10am has already passed, so this is how I would do it:
$timestamp = strtotime("2005-10-16 13:05:41");
// Get current hour and if it is > 10 add a day
if (date('G',$timestamp) >= 10) {
$tenAMTime = strtotime("+1 day 10am", $timestamp);
}
else {
$tenAMTime = strtotime("10am", $timestamp);
}
echo date('r',$tenAMTime); // Comment this out if you want

PHP one week ago from a specific date

so i have a date format like 07-09-10 and i want to know how to get ago from that date and if i can have a conditional like
if(is_date_with_1_week_of_above_date){
//do something
}
For checking date with relation to the Current Timestamp
if( strtotime( '-1 week' )>=$dateToCheck ) {
# $dateToCheck is within the last week
}
The other responses have good solutions for simple checking whether two date/times are within 1 week of each other - no point me repeating them.
Your date doesn't make clear the format (is it MM-DD-YY, DD-MM-YY, YY-MM-DD, etc.)? But an example using ISO 8601 date format is this:
$oneWeekAgo = strftime("%Y-%m-%d", strtotime("2010-07-09") - 60*60*24*7);
For a comparison, you can use the UNIX timestamp values
$date = "2010-07-09";
$compareDate = "2010-07-03";
$curTimestamp = strtotime($date);
$compareTimestamp = strtotime($compareDate);
if(abs($curTimestamp - $compareTimestamp) < 60*60*24*7)
{
// within 1 week
}
Edit
Per the comment on the date format, dd-mm-yy is a recognized format for dates, but mm-dd-yy is not in strtotime as seen here:
http://www.php.net/manual/en/datetime.formats.date.php
For it to work, you'd have to convert the dashes to slashes.
Also, if you're looking if the date is specifically one week prior,
$date = str_replace('-','/',"07-10-10");
$compareDate = str_replace('-','/',"07-03-10");
$curDate = strftime("%m/%d/%y", strtotime($date));
$compareDate = strftime("%m/%d/%y", strtotime($compareDate) + 60*60*24*7);
if($curDate == $compareDate)
{
// is one week prior
echo "OK";
}
Depending on your Time Zone, some days might have only 23 hours, so you can not use as a rule that a day has (60 seconds * 60 minutes * 24 hours) and with this, to calculate a specific date.
Specifying a date:
$specific_date = date( "Y-m-d" ); // for today
or
$specific_date = date( "Y-m-d", $timestamp ); // where timestamp is: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
The answer will be
$date = strtotime( $specific_date . " -1 week" );

Categories