I have a website which saves images into a database. I have successfully made a function that calculates the date that an image is added and this value is also saved into the database. I now want to calculate the date two weeks ahead from the addition date. This will show the date that the image file will cease to exist in the database.
I used the function:
$dateofaddedimage= date("d/m/Y");
This calculate thee current date of the addition of the image.
I am aware that there is the strtodate() function, but i don't think it will help.
Does anyone know how to add two weeks onto this function?
Thanks!
Add a number to time(), which is the current time stamp as seconds from the Unix Epoch.
$twoweeks = time() + (2*7*24*60*60);
$thatasdate = date("d/m/Y", $twoweeks) ;
Check out date_add and the PHP DateTime model.
From the php manual page comes this fine example:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
If you use this on your problem, you'd have
<?php
$dateofaddedimage = new DateTime('now'); //creates DateTime Model of today (and now)
$dateofimagedestroy = new DateTime('now'); //creates DateTime Model of today as well
$dateofimagedestroy->add(new DateInterval('P14D')); // adds 14 Days to the second date
The problem with what you are doing is that date() returns a string formatted to the date - not a proper date.
I would suggest either inserting a datetime into the database such as:
insert into yourTable (timeColumn) values (now());
This will insert the actual date. From there you can use mysql functions to add and subtract from this date.
Or using a timestamp in your code such as:
$uploadedTime=time();
From there you can either use PHP functions to add or subtract dates, or (as it is a timestamp) you can also use mysql functions to calculate what you need inside queries themselves.
Related
I am trying to get a simple line of text to appear if todays date is after another date.
I can either get it to appear on all pages or none, but I am unable to get it to display based on whether the challenge start date is before or after todays date. I believe it could be a date format issue, but everything I have tried has fallen short.
Here is my code:
Get todays date
$date_now = new dateTime();
Challenge start date
$challengeStartDate = date('dS F Y', strtotime($this->item->start_date));
echo '<!--' . strtotime('1970/1/1 00:00:00 +' . $validity) . '-->';
New text line
if ($challengeStartDate > $date_now) echo "New Text";
date() returns a string. With $challengeStartDate > $date_now it's like comparing if one string is bigger than the other (not sure if your dateTime handles that).
Your approach is otherwise fine. Just use timestamps to compare. time() gets you the time as a Unix timestamp:
$now = time();
if ($now > strtotime($this->item->start_date)) {
// do your thing
}
Something like this is more what you need. Try it out.
I had the very same problem some time ago.
All you need to do is store your local time in a database so it would be saved statically.
Because in your example, both $challengeStartDate and $date_now will change and update simultaneously and you wiill always get the current pc time!
Try storing it in a table or idk maybe sessions would help too.
I am using the following code to generate time stamps I always get different results for the same input on
$EventDateZone = new DateTime(str_replace('/', '-', $record['Date']), new DateTimeZone(Config::get('app.timezone')));
$EventDate = strtotime($EventDateZone->format("Y-m-d"));
$EventTimeZone = new DateTime($record['Time'], new DateTimeZone(Config::get('app.timezone')));
$EventTime = strtotime($EventTimeZone->format("H:i:s"));
The variables $record['Date] includes 01/04/2014 and $record['Time'] includes 09:26:00 AM and Config::get('app.timezone') is Asia/Dubai
The problem with the converted time it is always different here are the result for the converted time 1398576360 and 1398662760
I need the generated time stamp to be identical as this record will be stored in the database and I don't want to have it duplicated.
The date value does not contain a time, and the time value does not contain a date. UNIX timestamps however are compound values which are date-time values and must contain both. When you instantiate a new DateTime object and you give it an incomplete timestamp (i.e. missing either date or time), it fills in the missing values from the current time. And that will of course always be different.
You'll have to instantiate the DateTime object with both to get your UNIX timestamp. You cannot output a UNIX timestamp separately for date and time, because that doesn't make any sense. Your strtotime code doesn't make any sense. You can separate the DateTime object into date and time components again later if necessary, but not into two separate UNIX timestamps.
$event = DateTime::createFromFormat(
'd/m/Y H:i:s',
"$record[Date] $record[Time]",
new DateTimeZone(Config::get('app.timezone'))
);
echo $event->getTimestamp();
echo $event->format('Y-m-d');
echo $event->format('H:i:s');
I want to insert user entry log in a database table. The column where I want to keep the current date time is "date_time decimal(10,0) NOT NULL DEFAULT '0'". When inserting data I set the field as
$this->mytable->date_time = time();
My query executed successfully. But when I want to display the time of the entry it shows the time which is not match my pc(local server) time. To display the time I write
echo date('Y-m-d h:i:s A', $log->date_time);
I test several times but it showing the time which is 4 hours less than the exact time. On my test the current time is 2013-09-15 04:46:34 PM but table row shows 2013-09-15 12:46:34 PM.
Please help me. I can not find out the mistake.
You need to specify the timezone. The time() function will just retern a timestamp which is timezone-independent.
When you use the date() function you are using the server's timezone, I would recommend using the DateTime object:
$timezone = new DateTimeZone("Etc/GMT-4");
$date = new DateTime("#".$log->date_time); // #-symbol indicates timestamp input
$date->setTimezone($timezone);
echo $date->format("r");
Here is a list of supported timezones http://php.net/manual/en/timezones.php
Sorry. It was my mistake. When inserting data I set the time zone as
if(function_exists('date_default_timezone_set')) date_default_timezone_set("Asia/Dhaka");
But when display the data I forgot to set the time zone. It working fine when I set the time zone as I defined before in my display page. Thanks everybody for your help.
Try this
<?php
date_default_timezone_get();
echo time();
Manual
So im trying to insert a time using an input text field into a database table with data type TIME.
The format of time that I want to insert should be like this:
H:MM pm// example: 6:30 pm
The problem is the am/pm doesnt insert in my database table.
Only the hour and minute.
Please give me idea how to solve this.
Better with sample codes. Thanks.
Data Type TIME is for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
Store the TIME as a standard format (18:30:00), and the format it however you want when you display it (Using DateTime objects or the date functions).
MySQL doesn't support extra formats when storing time data.
I think you want to add the jquery time picker value in your database with actual format in the database.
Here I have written some function
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
http://php.net/manual/en/datetime.createfromformat.php
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
try .ToShortTimeString() after your date variable.
I am using PHP and MySQL, and want to calculate date time difference between two datetimes. I have a message table, in that table createdate is one field. I want to find out day and time difference from current date in the format 1 day 2 hours ago. What is the best way to go about this?
Use PHP's built in date functions:
<?php
$start_time = "Y-m-d H:i:s"; // fill this in with actual time in this format
$end_time = "Y-m-d H:i:s"; // fill this in with actual time in this format
// both of the above formats are the same as what MySQL stores its
// DATETIMEs in
$start = new DateTime($start_time);
$interval = $start->diff(new DateTime($end_time));
echo $interval->format("d \d\a\y\s h \h\o\u\r\s");
DateInterval documentation
DateTime::diff documentation
SELECT TIMESTAMPDIFF(HOUR,createdate,NOW()) as diff_in_hours FROM table1;
Then on php side you can easily convert the value of diff_in_hours to days + hours format.
You can use DATEDIFF() and TIMEDIFF() functions in MySQL.
SELECT DATEDIFF(CURDATE(), createdate) AS output_day,
TIMEDIFF(CURDATE(), createdate) AS output_time
FROM message_table
For output_day it is already in day unit. But output_time require additional manipulation to get the hour part of the time difference.
Hope this helps.