I'm implementing the following in PHP with stores this string in the DB for the "created_at" column: Wed, 17 Dec 2014 14:53:02 +1100.
$created_at = date('r', $insta['created_time']);
Now I'd only like to do the insert if $created_at is more than a certain date, for example:
if ($created_at > "Wed, 15 Dec 2014 00:00:00 +1100") { //insert in to db }
That doesn't work though. I'd usually use strtotime() but unsure how to go about it when the data is set in that format. Also, the column type is varchar(255).
Aside from your question, you need to store datetime data in a datetime column. Mysql has specific functions and formatting for this reason. But, in order to store it will need to be in Y-m-d H:i:s format. Doing this will save you big hassles down the road.
In regards to your question, you can use PHP's DateTime class if you are using PHP version 5.2+.
$compareTime = new DateTime('2014-12-15 00:00:00'); //new datetime object//
$createdTime = new DateTime($created_at); //converts db into datetime object//
if ($createdTime > $comparedTime) { ..insert into DB.. }; //compare them//
Why are you storing WED in database..just store date with the php function date("Y-m-d H:i:s") and the table field type will be date..
This way mysql automatically give u facility to compare two dates. You can simply use if else statement to compare two dates.
for PHP version >= 5.3
Use date_create_from_format or DateTime::createFromFormat
Although I think strtotime would also work but to be sure pass date time format and value to above function. For detail of format see documentation.
Use strtotime().
$created_at = strtotime($insta['created_time']);
$checkdate = strtotime(date('Y-m-d H:i:s')); //here we have to insert check date
if($created_at > $checkdate){
}
Related
Date and time from URL shall be in format YYYY-MM-DD-HH-II-SS. I want to check it correctness and insert into database in column of mysql timestamp format.
1) With $timestamp = $this->input->get('time_from') I receive it from url.
2) Before insertion in database I try to print it.
if (! empty($timestamp)) {
$timestamp = date_create_from_format('Y-m-d-H-i-s', $timestamp);
if (! $timestamp)
echo date_format($timestamp, 'Y-m-d H:i:s');
}
3) I obtain that year could be before 1970, for example 1950-01-01-10-32-01 is correct by this checking. But it is impossible to insert such date in mysql column.
4) I obtain that 2012-02-31-10-00-00 is transformed automatically in 2012-03-02 10:00:00.
The questions are: how to avoid these confusing automatic transformations? What is the best way to provide such checking?
You cannot store a date before 1970 in a MySQL TIMESTAMP column. Use DATETIME instead. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
You can use the checkdate() function to see if a date is valid.
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 want to input a timestamp in below format to the database.
yyyy-mm-dd hh:mm:ss
How can I get in above format?
When I use
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyy format
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
Thanks for answer
Not sure if this will help you, but try using:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
Technically, #stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.
Use PHP's native date function for that, e.g.
echo date('Y-m-d H:i:s');
or DateTime procedural API
echo date_format(date_create(), 'Y-m-d H:i:s');
or DateTime Object API
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
This is how i did it:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
this is i did it :
Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss'); method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
So today I used $date->get('YYYY-MM-dd HH-mm-ss'); as mentioned above. Seems to have solved my problem.
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html
The values that I received from my device are: 090211 = ddmmyy and 062123 = hhmmss in UTC.
But I found that the time is always 8 hours later if compared to the time that I need. It is because the time for Malaysia is +8:00. First I would like to add 8 hour, and finally I would like to store this kind of date format into my MySQL database as "2011-02-09 06:21:23". How would I convert these values?
To convert in PHP to a datetime, you will need the function DateTime::createFromFormat(); This function will return a DateTime.
Using this function you can also pass the timezone as a parameter.
Example:
$date = DateTime::createFromFormat( 'dmy Gms', '090211 062123', new DateTimeZone("Europe/Amsterdam") );
You can create a output a following:
echo $date->format('Y-m-d H:i:s');
or UNIX timestamp for the MySQL:
echo $date->format('U');
Hope this helps!
PHP has both localtime and gettimeofday functions, are you by chance using the wrong one (or misinterpreting its results)?
I want to convert user-submitted date format (mm/dd/yyyy) to a MySQL date format (YYYY-mm-dd). Submission is via a simple PHP form direc tto MySQL database.
$mysql_date = date('Y-m-d H:i:s', strtotime($user_date));
An alternative method as of PHP 5.2
$datetime = new DateTime($user_date);
echo $datetime->format('Y-m-d H:i:s');
DateTime is Y38k and timezone friendly.
A further method, this time on the SQL side is to use the convert method in your sql query:
CONVERT(VARCHAR(11),$user_date,111)
//e.g.
SELECT CONVERT(VARCHAR(11),DATEFIELD,111) AS DATE
//or
SET DATEFIELD = CONVERT(VARCHAR(11),'".$user_date."',111)
See: http://www.w3schools.com/sql/func_convert.asp - the number at the end changes the type of date format, with 111 returning: 2006/12/30.