strtotime() ads a minute of month change - php

I am adding a month to a string fromated date with this code:
$str ="2017-01-29 14:22:57";
$effectiveDate = strtotime("+1 months", strtotime($str));
echo "<br>";
echo date('Y-m-d h:m:s',strtotime($str));
echo "<br>";
echo date('Y-m-d h:m:s',$effectiveDate);
The output is:
2017-01-29 02:01:57
2017-03-01 02:03:57
I am wondering, why is there a minute change? It seems that every month there is a 1 min change.

I'll just pop this in as a community wiki; I don't want rep for this, nor should there be any made from it.
From the manual http://php.net/manual/en/function.date.php
m => m Numeric representation of a month, with leading zeros 01 through 12
You want i for minutes. Instead of h:m:s do h:i:s that's why.

You're formatting as hour:month:seconds, to have the timestamp you'll want to do:
echo date('Y-m-d h:i:s', $effectiveDate);
See the date documentation for more information.

Related

Today's date format - Day (Shorthand) Date Number Month (Shorthand) - PHP

I have a feed which gives feed in the following format: "Fri 14 Oct"
I want to see if today's date matches the date from the feed. My problem is the format of today's date/
$today = date("d m");
This outputs 17 10.
What is the best way to format $today so that it outputs Day (shorthand) space date (number) Month (shorthand) ?
how about:
$today = date("D j M");
As explained in date() reference manual.
Anyway you should be aware of timezone issues unless you are 100% sure that your server is in the same timezone of the feed you are comparing.
I would follow a different approach though, you can parse the feed's date using DateTime::createFromFormat() which also understand timezones, and then compare it with today's date.
$today = date("D d M");
PHP Date Documentation
<?php
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
For more details, please visit http://www.w3schools.com/php/func_date_date.asp

PHP datetime and strtotime got extra 3 minute

php date is get extra 3 minute after i use strtotime function. Please
<?php
$date=date('2014-03-03 09:00:00');
$date1=strtotime($date);
echo date('Y-m-d h:m:i',$date1);
?>
output: 2014-03-03 09:03:00
<?php
$date=date('2014-03-03 09:00:00');
$date1=strtotime($date);
echo date('Y-m-d h:i:s',$date1);
?>
you have used h:m:s , where m is month... :-)
Use the correct format of datetime.
Replace h:m:i with h:i:s
You are using m which is a numeric representation of a month (from 01 to 12)
That's why you are getting 03
change from
echo date('Y-m-d h:m:i',$date1);
to
echo date('Y-m-d h:i:s',$date1);

How to add months to a particular date

I just hope this question won't be marked as a duplicate because I've seen similar questions on stackoverflow but they all talk about adding days to the date, The problem here is that i want to add some particular months to a particular date which is gotten from my database I've tried adding it using strtotime() but the date just returns 1st January 1970, the code looks like this
<?php echo date('jS F Y', strtotime("$date +1 month")); ?>
//This is the value of date
$date = $student->date;
How to I add months to this particular date? Please note that the date is a timestamp in my database.Thanks
You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.
$date = new DateTime('#'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');
If you want to stick to using date() and strtotime() you would use this:
echo date("jS F Y", strtotime("+1 month", $student->date));
strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.
You should check out the documentation here,
But the just of it is the $date->add function. It allows you to add any amount of time to a timestamp using a DateInterval. Its a little tricky to get used to but here are a couple of examples:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
which outputs:
2000-01-01 10:00:30
2007-06-05 04:03:02
The date interval is formatted in years months days hours minuets seconds, simply put in the amount you want and it will add it, so in your case:
<?php
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
PHP's strtotime() function allows for a second parameter that allows you to set a relative date.
If you would like to add a month to tomorrow, here's how:
<?php
echo date("jS F Y", strtotime("+1 month", strtotime("2014-10-09")));
// returns: 9th November 2014

Php strtotime not showing the valid date

i have a mysql table with column type DATETIME, i want it to display like 19 Aug, 2013.
so i have tried with
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('y M ,Y',strtotime($date));
The output im getting is
2013-08-19 22:47:12
13 Aug ,2013
The i tried with
$datetime = DateTime::createFromFormat('Y-m-d', '2013-08-19');
echo $datetime->format('yM,Y');
But it also outputs the wrong date 13Aug,2013
Any one have faced the same kind of issue.
y is two-digit year, you want d, which is day. See also the documentation.
You used y twice:
echo $date = date('Y-m-d H:i:s');
echo '<br/>';
echo date('d M ,Y',strtotime($date));
To me it looks like it's doing exactly what it should - but the format specifier you are passing to date() and dateTime->format() looks strange - 'y' returns the year as 2 digits, 'Y' returns a 4 digit year. Did you mean that you wanted the day of the month at the start of he output?
Try 'd' or 'j' in place of 'y'.

PHP get 31 days distance from starting date

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));

Categories