Change today's date and time in php
The time()
functions actually asks the system clock for the time, so you'd have to update the time of the server's system clock. A few things to keep in mind:
- This is done differently on a Unix server than a Windows server. If you want code compatible with both, you have to be careful
- Sometimes (especially in shared hosting situations) you aren't allowed to change the system time
- Changes to system time will remain after the script has executed unless you change it back later. Therefore you risk really screwing up the system clock.
If you want to change the time globally for an entire app, I recommend having a static variable $timeOffset
and create your own time()
function as follows:
function myTime(){
global $timeOffset;
return time()+$timeOffset;
}
If you've taken all of that into account and still want to change the system time, however, you would need to send a command to the shell using shell_exec()
In Windows, the code would look like this:
shell_exec("date 09-09-99"); // Use "date mm-dd-yy" or "time hh:mm:ss", respectively
In UNIX, according to the date
man page, the code would look like:
shell_exec("date 0909hhmm1999"); // It says "date MMDDhhmiYYYY". I'm not sure how to set seconds, although I assume "mi" = "minutes"
NOW() function in PHP
You can use the date function:
date("Y-m-d H:i:s");
Is there a short way to modify DateTime to today but keep the previous stored time?
Not much better:
$newDate = new DateTime('today '.$date->format('H:i'));
Increase days to php current Date()
php
supports c style date functions. You can add or substract date-periods with English-language style phrases via the strtotime
function. examples...
$Today=date('y:m:d');
// add 3 days to date
$NewDate=Date('y:m:d', strtotime('+3 days'));
// subtract 3 days from date
$NewDate=Date('y:m:d', strtotime('-3 days'));
// PHP returns last sunday's date
$NewDate=Date('y:m:d', strtotime('Last Sunday'));
// One week from last sunday
$NewDate=Date('y:m:d', strtotime('+7 days Last Sunday'));
or
<select id="date_list" class="form-control" style="width:100%;">
<?php
$max_dates = 15;
$countDates = 0;
while ($countDates < $max_dates) {
$NewDate=Date('F d, Y', strtotime("+".$countDates." days"));
echo "<option>" . $NewDate . "</option>";
$countDates += 1;
}
?>
Changing value of database to current date and time after clicking button
You are trying to set "status" twice. That semicolon and nested single quotes could cause some issues as well where you have the date()
function.
I would recommend setting the date outside of the query and inserting it as a variable, like this:
$now = date('Y-m-d H:i:s');
$sql = "UPDATE `request`
SET `status` = '$now'
WHERE `reqnumber` = '$reqnumber';";
PHP date add 5 year to current date
Try with:
$end = date('Y-m-d', strtotime('+5 years'));
PHP date time greater than today
You are not comparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015
> 01/02/2016
because 09
> 01
. You need to either put your date in a comparable string format or compare DateTime
objects which are comparable.
<?php
$date_now = date("Y-m-d"); // this format is string comparable
if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}
Demo
Or
<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");
if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}
Demo
Add number of days to a date
This should be
echo date('Y-m-d', strtotime("+30 days"));
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
while date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
See the manual pages for
- http://www.php.net/manual/en/function.strtotime.php
- http://www.php.net/manual/en/function.date.php
and their function signatures.
Insert current date in datetime format mySQL
If you're looking to store the current time just use MYSQL's functions.
mysql_query("INSERT INTO `table` (`dateposted`) VALUES (now())");
If you need to use PHP to do it, the format it Y-m-d H:i:s
so try
$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");
Related Topics
Php/Apache: PHP Fatal Error: Call to Undefined Function MySQL_Connect()
No Mail Received in Inbox with Xampp 1.8.0, Mercurymail and Mail()
Simplexmlelement and Xpath, Getting Empty Array()
Laravel Expected Response Code 250 But Got Code "530"
Nginx + PHP-Fpm 7.1 - 504 Gateway Time-Out
Filter_Sanitize VS Filter Validate, Whats the Difference - and Which to Use
What Is Laravel Render() Method For
How to Fetch Account Data from a Provider with Lightopenid
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
How to Make a Select in PHP/MySQL Case Insensitive
How to Run from PHP a Bash Script Under Root User
Using PHP, How to Insert Text Without Overwriting to the Beginning of a Text File
PHP $String{0} VS. $String[0];
How to Resolve the Error: SQL Authentication Method Unknown in Laravel-Mysql
File_Get_Contents Is Not Working for Some Url