i'm working on project where i'm doing this piece of code
date_default_timezone_set('Asia/Calcutta');
$login_time=>date('h:i:s');
now is there anyway i can include AM/PM value in my database? or i'm missing any step to do that :|
If you want to save with AM or PM, based on your given code , it should be written with 'A' inside date function like this:
date_default_timezone_set('Asia/Calcutta');
$login_time=>date('h:i:s A');
A = Uppercase Ante meridiem and Post meridiem.
Read more about date function here.
Though I'd recommend using the database to format your date, it is also possible using the PHP Date function. To wit:
$login_time_as_string = date ("h:i:s A", $login_time);
If you need more assistance, do leave a comment.
Related
I have a variable pulled from a SQL DB which is in the "datetime" format of:
2017-02-22 16:24:12
I will be needing to find a file in this directory based on the results of the remaining code, but im not sure how to format the date variables for the directory.
Eg:
/basefolder/2017/02/22/File.extension
I have read lots about using date variables, but they are all based on the current date. My thoughts were that I could just strip the numbers like
/basefolder/"first 4 numbers"/"second 2 numbers"/"last 2 numbers"/
VS trying to do this with any sort of date/time functions. But perhaps there is a date fuction I can/should use.
I do know that the datetime will always be formatted as such, but it feels "hackish" to do it the first way. I could do it in bash in a second, but I'm not so good w/ PHP.
Thanks for any help!
PS: To give context, I will be getting 2 variables passed to this php script, phone number and date. I will then query the CDR table for the Unique ID of this call record based on that information, then I know the file will be in:
/monitor/2017/02/22/*uniqueID*
Which I will then pass back to the original script to have it download the file.
You can use the function strtotime to convert your date string to a timestamp, and then use the date function to create the date formatted as you like, and you can pass the timestamp as the second argument (it only uses current time if you pass only the format)
docs for strtotime: http://php.net/manual/en/function.strtotime.php
<?php
$dateString = '2017-02-22 16:24:12';
$timestamp = strtotime($dateString);
$datepath = date('Y/m/d',$timestamp);
$path = "/basefolder/$datepath/";
var_dump($path);
This outputs: string(20) "/basefolder/2017/02/22/"
This code could be shortened to:
$path = "/basefolder/".date('Y/m/d/',strtotime('2017-02-22 16:24:12'));
It sounds like you already found the date function but there is more info on it here: http://php.net/manual/en/function.date.php
Note that you have to do it in two stages, as if you include the /basefolder/ part in the format passed to date, then it will replace individual characters with various formats of the date - e.g.:
$datepath = date('/basefolder/Y/m/d',$timestamp);
Sets $datepath to something like bpm12US/Pacificf2017Wednesday22US/PacificWed, 22 Feb 2017 16:24:12 -0800/2017/02/22
b is left alone because it means nothing, but then a is replaced with pm because of the time, s is replaced with the seconds value, e is replaced with the time zone and so on.
date_parse_from_format() will convert from string to broken down time array:
<?php
$d = date_parse_from_format("Y-m-d H:i:s", "2017-02-22 16:24:12");
$ds = sprintf("/base/%d/%02d/%d", $d['year'], $d['month'], $d['day']);
echo $ds;
This is my final code snippit!
<?php
$dateString = '2017-02-22 16:24:12';
$base = "/monitor/";
$path = "$base".date('Y/m/d/',strtotime("$dateString"));
echo $path;
?>
Based on the definition given on php.net about function date(), I tried to use it in the code like this:
echo"The date is :".date("l, d/m/Y, h:i:s a", time());
But the timestamp doesn't get printed. Why is this? I am unable to understand this part of the function.
To print timestamp just use time()
echo time();
I understood the problem:
I had to written the code properly but since It was not displaying current date and time, I was confused. I set the time zone to Asia/Kolkata. Now its working fine.
Thank you everyone for help.
<?php echo invoice_due_date($invoice); ?>
The above code outputs a date that is formatted as dd/mm/yyyy. I am looking for a way to take that date and then add or remove x amount of days from it and then print the result. I'm a novice when it comes to PHP so everything I have tried has failed. Thanks in advance!
Also, this code snippet is from a no longer supported project called "myclientbase" if it helps.
PHP5 has a nice class called DateTime.
You can initialize it from a string like this:
$date = DateTime::createFromFormat('d/m/Y', invoice_due_date($invoice));
Then, since PHP 5.3.0 this class has a method to add time amounts:
$date->add(new DateInterval('P10D'));
This adds 10 days to your date. See http://php.net/manual/en/datetime.add.php.
strtotime can translate the date to a time value (although you'd have to replace the slashes with dashes) and in the same operation add days:
strtotime(str_replace('/','-',invoice_due_date($invoice)) . ' + 1 day')
date can be used to format the date back to a suitable notation.
You can use the DateTime::add function. A full explanation and examples can be found at http://php.net/manual/en/datetime.add.php
$invoice->add(new DateInterval('P20D'));
This would add 20 days to the invoice date. You can simply run this before echoing out your $invoice variable.
How to take the time from date stored as 12/25/2012 5:12:05 AM .
date('l F j, Y, g:i a',strtotime($last_login_details[FL_DATETIME]));
This above function returned time as 12:00 am which should return 5:12AM.
FL_DATETIME has datatype DATE.
On database, the value is being stored like this :
12/25/2012 5:12:05 AM
According to the docs - http://docs.oracle.com/cd/B19306_01/server.102/b14220/datatype.htm#i1847 -
For input and output of dates, the standard Oracle date format is DD-MON-YY
That is most likely why $last_login_details[FL_DATETIME] is echoing 25-DEC-12
Try changing your query using TO_CHAR()
SELECT TO_CHAR(FL_DATETIME, 'MM/DD/YYYY HH24:MI:SS A.M.') AS FL_DATETIME ...
see http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html#date format
Solved my problem by :
SELECT TO_CHAR(FL_DATETIME, 'DD.MM.YYYY:HH24:MI:SS') FROM "FMS_LOG"
First of all, in my opinion, you should be storing all dates as unix timestamps. This makes it lot easier for you to do searches against times, and removes any inconsistencies that may arise from date string manipulation.
Second, I tested your code; it looks to be OK from what I can tell. Echo out what you are getting in the $last_login_details[FL_DATETIME] variable. The issue may lie in the variable assignment, and not the date string manipulation.
Hope that helps!
I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters