date function help - php

I have this invoice with todays date:
I want to display the expiration date (30 days later), is there a function that allows this?
or is it something simple like adding a +30 somewhere?
Help :D

$date = date();
$future = date_add($date, date_interval_create_from_string('30 days'));
is the procedural way to do it. There's also an OOP version documented here as well.

You can use a combination of date() and strtotime().
echo date('Y-m-d', strtotime('+30 days'));
For more information on specifying date formats, see the manual page for date().

I hope it will help you.
$thirtydaysadd = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "That day will be ".date("m/d/y", $thirtydaysadd);
mktime() is used to create new time stamp.

strtotime is probably your best bet.
echo strtotime("+30 days"), "\n";
http://php.net/manual/en/function.strtotime.php

When you face such problems, PHP online documentation is very useful.
Just for to http://php.net/keyword (for example, http://php.net/date) and documentation page for that keyword will be displayed. You could see Date/Time functions in the left sidebar which has the link to function date_add

Related

Get date from database in php

i am storing date and time in database using php using gmdate() function in format "Y-m-d H:i:s". for e.g.
2014-03-10 12:35:55
Now,on getting this data into a php variable,for e.g.
$temp=2014-03-10 12:35:55
can i extract and display only the DATE portion excluding the TIME portion??
I am new to date and time manipulation.Any help?
you can get directly the date from database like this
select DATE(column_datetime) as date from yourtable
As Pramod answered,
date('Y-m-d', strtotime($temp));
works.
date('Y-m-d', strtotime($temp));
The above statement returns the date from datetime format
The following code example is adapted by this thread: How to convert date to timestamp in PHP?
There you can also read why it is not safe to rely on strtotime (answer by daremon)
$temp = '2014-03-10 12:35:55';
$a = strptime($temp, '%Y-%m-%d');
$timestamp = mktime(0, 0, 0, $a['tm_mon']+1, $a['tm_mday'], $a['tm_year']+1900);
Edit
From the PHP manual
Note: This function is not implemented on Windows platforms.
Try this:
Echo date('Y-M-D', $var);

How to update only the month and year values of a date?

I have a static date 28-04-2012,. I want to update the only the month when the date gets to the 28th of the new month. So if the date is 28-05-2012, then 28-06-2012, 28-07-2012 and so on.
I would also like to update the year when I reach 2013. But always keep the date at 28? Can someone advise me how this would be done using js or php?
$current_date = "28-04-2012";
$next_month = strtotime($current_date . "+1month");
echo date('Y-m-d', $next_month);
the most simple trick i guess.
<?php
$date = '28-04-2012';
echo date('j-m-Y', strtotime("1 month", strtotime($date))) . "\n";
?>
See here: http://www.roseindia.net/tutorial/php/phpdate/php-date-add-1-month.html
$todayDate = date("Y-m-d");// current date
echo "Today: ".$todayDate."<br>";
//Add one day to today
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($todayDate)) . "+1 month");
echo "After adding one month: ".date('l dS \o\f F Y', $dateOneMonthAdded)."<br>";
People are suggesting strtotime(), but I have to say I have a strong dislike for this function. It isn't designed for date arithmetic, but it was the only option available in older versions of PHP, so everyone uses it for that.
A better solution can be found in the PHP5 DateTime class, and in particular the DateTime::Add method (also known as the date_add() function).
The only reason to use strtotime() instead ofDateTime::Add()is if you're using a version of PHP older than 5.3, since this is when theAdd` method was added. But if that's the case, your first priority should be to upgrade your PHP.

Remove time part of a timestamp

How can I remove time part of a timestamp?
So for example turn 1310571061 to 1310565600 which is the plain date timestamp.
strtotime(date("Y-m-d", 1310571061));
That should do it for you.
In case you wanted a mathematical solution:
$time=1310571061;
echo floor($time/86400)*86400;
There are 86,400 seconds in 24 hours (which is the length of a typical day, but not all... see DST). Since our timestamps are in UTC, this shouldn't be a problem. You can divide that by the number of seconds in a day, drop the remainder, and multiply back out.
Using a DateTime object can be helpful, and also can make the code more readable...
$dt = new DateTime();
$dt->setTimestamp(1310571061);
echo $dt->format('Y-m-d, H:i:s') . "\r\n";
$dt->setTime(0, 0, 0);
echo $dt->format('Y-m-d, H:i:s');
Result...
2011-07-14, 03:31:01
2011-07-14, 00:00:00
The choice of whether to use raw timestamps or DateTime objects will depend a lot on the implementation needs, but generally speaking DateTime objects will go a long way towards reducing confusion and errors that can crop up especially around Timezone issues and Daylight Saving Time issues.
Try:
<?php
$ts = '1310571061';
echo strtotime(date('Y-m-d 00:00:00', $ts));
?>
$pubdate='2003-02-19T00:00:00.000-05:00';
$da = strtotime($pubdate);
echo $dat = date('Y-m-d', $da);
The answer is like :"2003-02-19"
Thank You
You could do:
$date = strotime(date("y/m/d", $timestamp));
This is what I usually do:
your_timestamp = pd.to_datetime(your_timestamp.strftime(format="%x"))
The function strftime will convert your_timestamp to a string without the time component. Then the function pd.to_datetime will convert it back to a Timestamp without the time component.

How to I accurately get current UTC time via strtotime?

In PHP, how do I get the current time, in UTC, without hard coding knowledge of where my hosting provider is?
For example, I tried the following:
time() + strtotime('January 1, 2000')-strtotime('January 1, 2000 UTC')
and find that it reports a time that is one hour ahead of actual UTC time. I tried this on two different hosting providers in two different time zones with the same results.
Is there a reliable (and, hopefully, cleaner) way to accurately get the UTC time?
I am limited to PHP 4.4.9 so I cannot use the new timezone stuff added to PHP5.
Thanks, in advance.
$time = new DateTime('now', new DateTimeZone('UTC'));
echo $time->format('F j, Y H:i:s');
This seems to work for me. Of course, you'll need to test it on PHP 4 since all of my servers have PHP 5, but the manual claims this should work for PHP 4.
$t = time();
$x = $t+date("Z",$t);
echo strftime("%B %d, %Y # %H:%M:%S UTC", $x);
First time around, I forgot that the date could change between the call to time() and date().
Does this work for php 4.4.9?
echo gmdate('Y-m-d H:i:s', time());
or if you want it for a specific date:
$time = strtotime('January 1, 2000 UTC');
if($time){
echo gmdate('Y-m-d H:i:s', $time);
}
$utcTtime = gmmktime();
$unixTimestamp = time();
gmmktime: Get Unix timestamp for a GMT date

Date to day conversion in php

in my webpage i need to calculate the day[ie. SUN,MON,TUE...] from the date .. The date is in ['06/22/2009'] this format ? How can i calculate it in to day[That is it will show me MON] in php . Please help me to find out . Thanks in advance..
First, you need to parse the string '06/22/2009' into a timestamp, possibly using strtotime():
$dt = strtotime('06/22/2009');
Then, you can format the timestamp using date():
$day = date("D", $dt);
If you especially want it in uppercase, use strtoupper():
print strtoupper($day);
For future viewers, I think this will be more helpful.
echo date('l', strtotime('11/20/2017'));
Use the date
http://au.php.net/manual/en/function.date.php
and strtotime http://au.php.net/strtotime
functions
pls check the funcion. the out put is showing the current day.pls tell me the given date dau in the date("D",$dat)

Categories