I am working on a WordPress plugin that allows the user to select a time using a jQuery dropdown script. The time is a string in the format of "5 : 34 PM". I need to save that value to my mysql database in the datetime format.
So far, I can save the date and time but the AM/PM is not being factored in.
Here is my PHP function:
function db_tables_insert() {
global $wpdb;
$table_name = $wpdb->prefix . 'tweettweet';
$tweet = $_POST["tweet"];
$time = $_POST["timepicker"];
$time=preg_replace('/\s+/', '', $time);
$date = "2015-02-08:";
$datetime = $date.$time;
$wpdb->insert(
$table_name,
array(
'time' => $datetime,
'text' => $tweet,
)
);
}
In this example, the value saved to the database would be "2015-02-08 12:13:00" (assuming the user selected 12:13 for the time). The problem is, that value is the same whether the user selects 12:13 am or 12:13 pm.
I need a way to convert the string so that the "am/pm" is taken into consideration when saved to the database.
strtotime should be able to handle this.
echo date('Y-m-d H:i:s',strtotime('2014-12-12 12:34 AM'));
returns 2014-12-12 00:34:00
Related
I am trying to select a record from specified date
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday));
But i am not getting any record even if there are entries in table. I also want to make sure that query select the only record which was created on specified date.
Any help will be appreciated..
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday, 'timestamp <' => date('Y-m-d')));
If you share your table schema and sample data, I can give you correct answer. Still I can guess you are compairing date string with timestamp.
The code $yesterday = date('Y-m-d',strtotime("-1 days")); will return $yesterday value as '2017-04-10'. But actually in your database you are compairing with timestamp field, which hold the timestamp in numeric value.
You can use php strtotime function to convert any date to respective time stamp. strtotime($yesterday).
Correct Code will be :
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => strtotime($yesterday)));
Again please make sure, your database field timestamp is storing only date in form of timestamp.
Another solution is, you can use mysql date compare functions.
What I want is to make a database update with codeigniter where I put a value to current timestamp:
My settings array:
$settingsMachineUpdateEvent[] = array(
'id' => $eventId,
'etid' => '5',
'insert_by' => '2',
'modified_date' => $currentTime
);
And my $currentTime:
$currentTime = date_default_timezone_get();
And I send the update like this:
$this->db->update_batch('balance_events', $settingsMachineUpdateEvent, 'id');
My problem is the $currentTime var, I want to assign it the current time stamp so I can update my modified_date column from mysql with the current timestamp value. I also try'ed with NOW(), time(), and assign it directly from array, nothing worked, I will get 0000-00-00 00:00:00 after the update. Where is the issue? Thanks!
Instead of this:
$currentTime = date_default_timezone_get();
try
$currentTime = date('Y-m-d H:i:s'); // for current time
$currentTime = time(); // for current timestamp
Explanation: date_default_timezone_get() is used to get the timezone, but in your case you need the time or timestamp to store in the table.
I am trying to check if one date is equal than the other date, but I can't get the match because the date format coming from the form turns into a different order once it gets through the "parse" code.
I need to format this date to find the match, here is a sample code to show how I am trying:
...
// $ago will give me this date: 2016-12-09 00:00:00
$ago = Carbon\Carbon::today()->addDays(2); // Todays date + 2 days
//$request->datex has the date coming from a form with this format, '12-06-2016'.
// Once a parse $request->datex here, the date gets out of order:
$my_date = Carbon\Carbon::parse($request->datex);
// it shows the date like this, 2016-09-12 00:00:00 , I need it to be on this format: 2016-12-09 00:00:00
// then I could do this:
if ( $ago$ == $my_date ) {
dd($my_date.' is equal to: '.$ago );
}else{
dd(' Not equal!');
}
...
Thanks for looking!
Change this line
$my_date = Carbon\Carbon::parse($request->datex);
with this:
$my_date = Carbon::createFromFormat('m-d-Y', $request->datex)
I've assumed that your format '12-06-2016' means DAY-MONTH-YEAR
UPDATE
Tested my solution on my machine and it works, date is recognized properly:
When
$request->datex = '12-06-2016'
then
$my_date = \Carbon\Carbon::createFromFormat('m-d-Y', $datex);
gives me date like that: public 'date' => string '2016-12-06 18:52:09.000000' (length=26)
Date has been parsed properly. The thing that I've assumed just now. These dates won't be same cause of hours, minutes, seconds and milliseconds. To fix that just we have to compare dates that way:
if ( $ago->format('Y-m-d') == $my_date->format('Y-m-d') )
//do something awesome with our equal dates
PHP expects DD-MM-YYYY or MM/DD/YYYY formats.
If you always have a MM-DD-YYYY format, you could do this before parsing:
$request->datex = str_replace('-', '/', $request->datex);
I'm trying to store user signup date and time using codeigniter 3.0.6 and using
NOW()
as
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', 'NOW()');
$this->db->insert('doc_users');
but it is not storing date time in database
see database image
use date() like this
date('Y-m-d H:i:s'); # output 2015-12-22 16:41:25
Final Code is
date_default_timezone_set('Asia/Karachi'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', $now);
$this->db->insert('doc_users');
this works for me in Codeigniter 3.0.6
$this->db->set('user_registered', 'NOW()', FALSE);
try this
first load date helper
To follow ci structure you have to use mdate() function otherwise you can also use date function
$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', mdate("%Y-%m-%d %H:%i:%s"));
$this->db->insert('doc_users');
I would recommend using load the date helper.
mdate('%Y-%m-%d %H:%i:%s', now());
There is no need to use multiple sets you could simply put it in a array.
$data = array(
'user_nicename' => $nameVar,
'user_login' => $emailVar,
'user_pass' => $passwordVar,
'user_registered' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->set($data);
$this->db->insert('doc_users');
Also make sure your user_registered database type is DATETIME
To set your current date_time_zone
Go to the config.php and add the code below
Example: date_default_timezone_set('Pacific/Auckland');
For your time zone you can find list here http://php.net/manual/en/timezones.php
$data = [
'type'=>$this->input->post('txttype')
//for getting input value of form
];
$this->db->set('column_name', 'NOW()', FALSE);
$this->db->insert('table_name', $data);
Also you can make a DB query to get current DB date/time:
$this->db->select('NOW() as db_date')->get()->row()->db_date
In Codeigniter, you have to load date helper as
$this->load->helper('date');. than after you have to define city for
timezone as date_default_timezone_set('Asia/Kolkata');.than after
you can set your time zone according your requirnment.
$this->load->helper('date'); // load Helper for Date
date_default_timezone_set("UTC");
echo $date=gmdate("F j, Y").'<br>'; // ie. May 23, 2018
if (function_exists('date_default_timezone_set'))
{
date_default_timezone_set('Asia/Kolkata'); // Specify your time zone according to your city
}
date_default_timezone_set('Asia/Kolkata'); // Defined City For Timezone
$currentDate =time();
$datestring = '%Y-%m-%d - %h:%i %a';
$time = time();
$better_date= mdate($datestring, $time).'<br>'; // i.e : 2018-05-23 - 09:52 am | For AM | PM result
$c_date=date("Y-m-d H:i:s").'<br>'; // 2018-05-23 09:52:36 | For Seconds Result
// Insert Date and Time to the Database
$data=array(
'name' =>'Rudra',
'email' =>'test#me.com',
'city' =>'Kolakata',
'data_time_on'=>$c_date
);
$this->db->insert('tableName',$data);
I need to convert this query from php to mongoDB query
$query = "select * from table where data_added like '%data%';
I have date stored in variable
$date = "2013-09-02";
and in my mongo Document the date sorted as :
$dateAdded = new MongoDate(strtotime('2013-09-02 12:21:55'));
I tried
$date = new MongoDate(strtotime("$date"));
$mongo->find(array('date_added'=>array('$lt'=>$date)));
and
$mongo->find(array('date_added'=>$date));
but without success .
so I need to query usin (Y-m-d) not (Y-m-d h:i:s)
so how to use LIKE query for data in mongo
Thanks
You need to do a range query. Create a timestamp, for example using strtotime(), to get the unix timestamp at the start of the day, and another one and the end of the day.
Depending on if you want these two ends inclusive or exclusive, you then use
// Both points/seconds inclusive
->find(array("date" => array('$gte' => $startOfDay, '$lte' => $endOfDay)));
// Both seconds exclusive
->find(array("date" => array('$gt' => $startOfDay, '$lt' => $endOfDay)));
See http://cookbook.mongodb.org/patterns/date_range/