inject date string as srtotime - php

I am trying to resolve this since hours, and I tried different solutions offered here within the website, but for some reasons the website always returns an error when applying the suggestions.
I have a form field which uses a datepicker, and the output is a date in the format: November 6th, 2013
Now, the mysql database and the rest of the script only works with a ten digit date format, which I have been told is the unix timestamp format and that it can be converted using the strtotime. So I am trying to convert the string into strtotime, but cant resolve this. For now I have:
$insertData['enddate'] = $this->input->post('openDays');
which returns the date "November 6th, 2013" and I changed it to
$insertData['enddate'] = strtotime $this->input->post('openDays');
which returns the error: Parse error: syntax error, unexpected T_VARIABLE in /home/xxx/public_html/app/controllers/project.php on line 214
any suggestions on how to correctly apply this?
Thanks

Try this, you are doing a mistake by using strtotime.
$insertData['enddate'] = strtotime($this->input->post('openDays'));
Reference link: Strtotime PHP

strtotime is a function , so you need to enclose it like this (using parenthesis)
$insertData['enddate'] = strtotime($this->input->post('openDays'));
-------^ -------^
EDIT :
I suggest you use DateTime instead of strtotime
$dt = DateTime::createFromFormat('H:i', '17:00');
$insertData['enddate']=$dt->format('H:i');

Related

how to compare two dates in if condation in laravel blade?

in my controller i used Carbon to get current timestamp like showing below:
$current_timestamp = Carbon::now()->format('j/n/Y');
the output of the above:
18/8/2022
and i am getting data from external API like showing below (from blade):
$data[0]['DocDate']
the output of the above:
18/8/2022 12:00:00 AM
now i want to remove 12:00:00 AM from it
i tried in blade view to do:
{{Carbon\Carbon::parse($data[29]['DocDate'])->toDateString()}}
but i am getting this error:
Could not parse '18/8/2022 12:00:00 AM': Failed to parse time string (18/8/2022 12:00:00 AM) at position 0 (1): Unexpected character
and i tried:
$data[29]['DocDate']->format('j/n/Y')
and i get this error:
Call to a member function format() on string
how can i overcome this issue?
You can use create from format function to change the format of incoming date as below:
$inDate = $data[0]['DocDate'];
$outDate = Carbon::createFromFormat('d/m/Y h:i:s a', $inDate )->format('d/m/Y');
On the principle of Keep It Simple - Why not just compare them as strings?, the longer one can easily be shortened, then just compare.
Accepted this is not a "purest" approach, but the format of a "standard" date is not likely to change.
now i want to remove 12:00:00 AM from it
So you don't actually care about date, you don't need Carbon or parsing according to format, you just need to keep the first word of your string:
{{ explode(' ', $data[0]['DocDate'])[0] }}

DateTime::createFromFormat issue with particular date/time

I have been using the following code for a couple of months and it's been working fine, but now seems to have an issue with a certain time or time range.
I have a GPS tracker that sends it's date/time like this:
150102235335
The format is ymdhis
$input_array[6] = 150102235335;
$datetime = DateTime::createFromFormat('ymdhis', $input_array[6]);
$datetime = $datetime->format('Y-m-d h:i:s');
Using that time, php crashes with the following error:
PHP Fatal error: Call to a member function format() on a non-object
It seems that datetime ends up empty.
However, using the time 150103004933 works just fine.
Can anyone see where I have gone wrong here, or is this a bug?
Is there a better way to accomplish my date conversion?
I am using PHP 5.4.35
h is 12-hour format; and therefore 23 hours is invalid. Only values in the range 00-11 would be valid.
H is 24-hour format

PHP mktime notice

I want to change given date and time or date only into Unix time.
I tried like this:
mktime("Jan-12-2012 2:12pm");
But it’s not working:
Even in PHP documentation I looked at many examples and many of them don’t consist the matter that I want.
And when I try:
$user_birthday=$_POST["user_birthday"];
$db_user_birthday=empty($user_birthday)?"":mktime($user_birthday);
$_POST["user_birthday"] was given value from form that is jan-12-2012 2:12pm
it show error like this:
Notice: A non well formed numeric value encountered in C:\Program
Files (x86)\Ampps\www\admin\index.php on line 76
How do I fix it or display time into Unix?
Use this one:
date("M-d-Y h:i:s", strtotime($user_birthday));
You should be using strtotime instead of mktime:
Parse about any English textual datetime description into a Unix
timestamp.
So your code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
Then you can process that date like this to get it formatted as you want it to:
echo date("M-d-Y h:ia", $db_user_birthday);
So your full code would be this:
$user_birthday = $_POST["user_birthday"];
$db_user_birthday = empty($user_birthday) ? "" : strtotime($user_birthday);
echo date("M-d-Y h:ia", $db_user_birthday);
Note I also added spaces to your code in key points. The code will work without the spaces, but for readability & formatting, you should always opt to use cleaner code like this.
You should take a look at this answer: convert date to unixtime php
Essentially, you have mixed up mktime() with strtotime(). strtotime() allows you to parse an English textual string into a Unix timestamp. mktime() constructs a unix datetime based on integer arguments.
For example (again taken from the question above)
echo mktime(23, 24, 0, 11, 3, 2009);
1257290640
echo strtotime("2009-11-03 11:24:00PM");
1257290640

How to format date in PHP from a string of concatenated numbers?

I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.

CodeIgniter: format mysql DATETIME field dd/mm/yy hh:mm

Hi pretty much what it says on the tin.
I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.
I've tried:
<?php
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring,$row->created);
?>
But I'm getting an error:
Message: A non well formed numeric value encountered
Any help most appreciated!
Cheers,
Billy
Try:
echo date ("d/m/Y h:ia",strtotime($row->created));
The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));
The only difference between mdate() and date() is, as the CodeIgniter docs say:
This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.
The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.
Got this to work using treeface's solution, with one minor change:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created));
//strtoDATE didn't work but strtoTIME did
Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.
HTH
I'm using:
mdate(date_string,mysql_to_unix($row->created))
That should work.

Categories